You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use accesors and mutators instead of getters and setters.
Class members should be private.
Private members can be modified from the editor using [SerializeField].
Naming
variables in lowerCamelCase
methods in UpperCamelCase
interfaces should start with I (e.g. ICanMove)
enums should start with E
Considering using events when necessary (e.g. when you kill an enemy) avoid them in Update functions, they are fairly costly in Unity.
e.g.
classFoo:MonoBehaviour{/** * Firsty add the members */// This can be modified from the Editor[SerializeField]privateinteditableField;// This can never be changed "by hand"privateint_privateField;// Add accesor and mutator for _privateField;publicintPrivateFieldButPublic{get=>_privateField;set=>_privateField=value;}/** * Delegate has a GameObject parameter to distinguish * between multiple instances of class Foo, as the * event is static. (instance might as well be a string) */publicdelegatevoidSomeEvent(GameObjectinstance);/** * Only methods that have the same signature * as SomeEvent can subscribe to onEvent */publicstaticeventSomeEventonEvent;// Fires event when colliding with somethingprivatevoidOnCollisionEnter(Collisioncollision){// ? means don't crash if there is no-one subscribedonEvent?.Invoke(this);}}classBar:MonoBehaviour{[SerializeField]privateFoofoo;/** * Can be directly use like this * Can only be set in this class */publicintAccumulator{get;privateset;}voidStart(){Foo.onEvent+=OnDeath;}// Bar considers foo dies when firing privatevoidOnDeath(GameObjectinstance){FoofooInstance=instanceasFoo;if(fooInstanceisnull){return;}Accumulator+=fooInstance.PrivateFieldButPublic;}}
Do not create monolithic classes, instead extract functionality that might be reusable into an Interface (even if there currently no case for its reusability in the project).