Changes

Jump to navigation Jump to search
No change in size ,  12:06, 7 October 2022
no edit summary
Line 1: Line 1:  
For those of you coming from a more traditional academic programming language/software, Unity may be a little confusing in terms of when and where things happen. Unlike, say, Python, there is no main loop to be called explicitly. Instead, Unity scripts are derived from the [https://docs.unity3d.com/ScriptReference/MonoBehaviour.html MonoBehaviour] class, which handles the [https://docs.unity3d.com/Manual/ExecutionOrder.html order of execution] and any update loops per object. Below is a list of key functions that you will likely have to use to get things done or measured.
 
For those of you coming from a more traditional academic programming language/software, Unity may be a little confusing in terms of when and where things happen. Unlike, say, Python, there is no main loop to be called explicitly. Instead, Unity scripts are derived from the [https://docs.unity3d.com/ScriptReference/MonoBehaviour.html MonoBehaviour] class, which handles the [https://docs.unity3d.com/Manual/ExecutionOrder.html order of execution] and any update loops per object. Below is a list of key functions that you will likely have to use to get things done or measured.
    +
==Functions==
 
===Awake()===
 
===Awake()===
 
Called when the object the script is attached to is first loaded into a scene. This is where you would initialize any variables or states that you may need before the application starts, and where to set up a [https://en.wikipedia.org/wiki/Singleton_pattern#Unity Singleton], for example. If there are multiple MonoBehaviour scripts in the scene, the order in which they call Awake() is not deterministic, so if one script needs to interact with another, ideally you will set that up in the Start() function.
 
Called when the object the script is attached to is first loaded into a scene. This is where you would initialize any variables or states that you may need before the application starts, and where to set up a [https://en.wikipedia.org/wiki/Singleton_pattern#Unity Singleton], for example. If there are multiple MonoBehaviour scripts in the scene, the order in which they call Awake() is not deterministic, so if one script needs to interact with another, ideally you will set that up in the Start() function.
   −
==Functions==
   
===OnEnable()===
 
===OnEnable()===
 
Called every time the script is enabled. If the script is enabled at the start of the scene, this will fire after Awake(), but potentially before the Awake() of a different script. Use this function to initialize objects that may be disabled and enabled multiple times during the game, or objects that get instantiated during runtime.
 
Called every time the script is enabled. If the script is enabled at the start of the scene, this will fire after Awake(), but potentially before the Awake() of a different script. Use this function to initialize objects that may be disabled and enabled multiple times during the game, or objects that get instantiated during runtime.

Navigation menu