October 29, 2019
Beginner Programming: Unity Game Dev Courses
Beginner Programming: Unity Game Dev Courses
Unity Learn Course – Beginner Programming
Tips & Considerations
Unity’s Order of Events
Unity’s Order of Events:Awake : OnEnable : Start : Update : OnDisable : OnDestroy
- Awake: first function called when object is instantiated; true whether added to scene in editor or instantiated in code
- OnEnable: event; fired when enabled gameObject is instantiated or when a disabled object is enabled
- Start: after Awake, and OnEnable, but before the 1st frame update
- Update: happens every frame after initialization methods
- FixedUpdate: frame-independent and occurs before physics calculations are performed
- LateUpdate: called once per frame after update method has completed execution
- OnDisable: event; fires when object is disabled, or before it is destroyed
- OnDestroy: execute when object is destroyed in code, or when scene containing it is unloaded
Reference Caching: creating a field for a reference and getting that reference once during initialization to use whenever that object is needed
This information is very useful to understand when you start referencing a lot of objects throughout your code and have a lot of pieces working together. This will help with avoiding errors, and debugging when those types of issues do come up. This is something I ran into a lot when working on my scene manager project as getting references was a bit of a pain and the timing was very crucial.
Using Attributes
Attributes (C#): powerful method of associating metadata, or declarative information, with codeUnity provides number of attributes to avoid mistakes and enhance functionality of inspector.
Some common attributes used in Unity include:
- SerializeField: makes a field accessible to the inspector
- Range
- Header
- Space
- RequireComponent
Public fields are inherently accessible by the inspector, but many other access modifiers hide the field from the inspector (like private). To get around this, you add the SerializeField attribute.
The RequireComponent attribute takes in a type and automatically adds that component type to the same gameObject whenever this object is placed. It also ensures that the other component cannot be removed while this component exists on the gameObject.