Unity Event Functions Order
One of the important thing while coding games in Unity is to know when and for what using the default Unity functions.
Lets learn this new stuff and make a little practice with .
The following is showing the exact order of the default functions and for what you would use each function:
I - Initialization :
all the following functions are called once per frame
1 ) void Awake ( ) : is used for initializing variables or game state before the game starts , you can use it instead of the constructor and it would never act like a coroutine, after all objects are initialized you can speak to them using awake and communicate to through tags eg :
2 ) void OnEnable ( ) : OnEnable / OnDisable these to functions must called when the attached gameObject is changed to enable ( active ) or disable ( deactivate)
3 ) void Start ( ) : is used to getting components as explained in the previous article find link here .
II - Physics :
1 ) FixedUpdate ( ) : FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.
2 ) void OnTrigger...( ) : used when two game objects collide with each other where both game object must have a collider component .
3) void OnCollision... ( ) : is called when this collider/rigidbody has begun touching another rigidbody/collider.In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider.
III - Input Events :
1) void OnMouse ... ( ) : Mouse events occur when you interact with the UI using a mouse. Touch, pens, or other pointing devices . For more details visit unity documentation.
IV - Game Logic :
1 ) void Update ( ) : we already talked about Update here , Update is the most commonly used function to implement any kind of game script, but not every MonoBehaviour script needs Update
2) Coroutines : Coroutines mean that you can pause the script for some seconds till another function finishes the execution , the implementation is as following :
- 1) void yield null ( ) .
- 2) void Yield WaitForSeconds ( ) .
- 3) void Yield StartCoroutine ( ) .
3 ) void LateUpdate ( ) : LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
V - Rendering :
Executed once every frame and should be used to render related calls , post processing and user interface
VI - Pausing and Quitting :
- 1 ) void OnApplicationPause( ) : To pause the game .
- 2 ) void OnApplicationQuit() : To quit the game .
VII - Cleaning up :
the last event function order that could be used for pooling system and UI calls , we have the following :
- 1) void OnDisable( )
- 2) void OnDestroy( )
That was all about the execution order of the Unity event functions , you can find for more details about what I explained here or here .
Congrats you are learning new things each time .
Comments
Post a Comment