Awake() Function in Unity - Unity Scripting API MonoBehaviour Tutorial 02

Опубликовано: 28 Сентябрь 2024
на канале: ChidresTechTutorials
4,080
97

Notes for You: Awake() Function in Unity - Unity Scripting API MonoBehaviour Tutorial 02

- We know that In Unity everything is an object.
- Because everything in Unity inherits from a root base class called Object.
- When you run a game application, first scene of the game is loaded.
- All objects of the scene are created and initialized first.
- After all objects are created and initialized;
- Awake method of Unity Script is called and executed.

Awake method:
- is an initialization method
- is called first
- is called only once in a script life cycle
- is executed even if script is not enabled
- is best for initializing the current object’s properties or variables; which do not depend up on other object’s property or variable value.
- if you want to initialize the current object’s properties or variables, which depend up on other object’s property or variable value, then you must initialize them inside Start method because Start methods are called after all Awake methods.

Ex: If you want to initialize the player velocity same as enemy velocity, then initialize enemy velocity inside Awake method of enemy’s script and initialize player velocity inside Start method of player’s script because here player’s velocity property depends upon enemy’s velocity property value.

Note: Each object’s Awake() is called in a random order i.e. there is no guarantee in order of executing Awake(); Hence it is recommended to avoid code in Awake() which relies on other object’s property or variable value.

Example Code:
void Awake()
{
Debug.Log ("Awake");
}


=========================================

Follow the link for next video:
Unity Scripting API MonoBehaviour 03 - OnEnable() Function in Unity
   • OnEnable() Function in Unity - Unity ...  

Follow the link for previous video:
Unity Scripting API MonoBehaviour 01 - Introduction to MonoBehaviour in Unity
   • Introduction to MonoBehaviour Class -...  

=========================================

Unity Scripting API - MonoBehaviour Tutorials:-
   • Unity Scripting API - MonoBehaviour T...  

=========================================
Watch My Other Useful Tutorials:-

Unity C# Scripting Tutorials:-
   • Unity C# Scripting Tutorials  

Unity Scripting API - Introduction Tutorials:-
   • Unity Scripting API - Intro Tutorials  

Unity Scripting API - Time Tutorials:-
   • Unity Scripting API - Time Tutorials  

Unity Scripting API - Transform Tutorials:-
   • Unity Scripting API - Transform Tutor...  

Unity Scripting API - Input Tutorials:-
   • Unity Scripting API - Input Tutorials  

Unity Google VR Tutorials:-
   • Unity Google VR Tutorials  

=========================================

► Subscribe to our YouTube channel:
   / chidrestechtutorials  

► Visit our Website:
https://www.chidrestechtutorials.com

=========================================
Hash Tags:-
#ChidresTechTutorials #UnityScripting #UnityScriptingTutorial #UnityScriptingAPI #Unity