welcome here .Unity Learning,Unity Training,UnityEnterprise Training and education area, there are manyU3D Resources,U3D Training Video,U3d Tutorials,U3d Frequently Asked Questions,U3d Project Source Code, we are committed to building the industryUnity3dTraining, learning the first brand.
Unity3d beginners often confuse awake with start.
Briefly, awake is called immediately after the Monobehavior is created, and start will be executed when Monobehavior is created, before the frame is updated, in the case of the monobehavior.enabled = = True.
void Awake () {
}
The initialization function, which is called automatically at the beginning of the game. Commonly used to create variables and things like that.
void Start () {
}
The initialization function, after all the awake functions have run (typically, but not necessarily), is automatically used before all the update functions. Typically used to assign values to variables.
The script that we usually write does not define [Executeineditmode] This attribute, so awake and start are only executed in runtime
Example 1:
public class Test:monobehaviour {
void Awake () {
Debug.Log ("Awake");
Enabled = FALSE;
}
void Start () {
Debug.Log ("Start");
}
}
The above code, in awake we call the Enabled = FALSE; This monobehavior update is forbidden. Since start, Update, Postupdate, and so on are part of the runtime behavior, this code will cause start to not be called.
In the course of the game, if there is another set of code that has the following call:
Test test = go. Getcomponent<test> ();
Test.enabled = true;
At this point, if the start function is not triggered before the monobehavior, it will be triggered after the code executes.
Example 2:
Player.cs
Private Transform handanchor = null;
void Awake () {Handanchor = transform. Find ("Hand_anchor"); }
void Start () {Handanchor = transform. Find ("Hand_anchor"); }
void Getweapon (Gameobject go) {
if (Handanchor = = null) {
Debug.logerror ("Handanchor is null");
Return
}
Go.transform.parent = Handanchor;
}
Other.cs
Gameobject go = new Gameobject ("Player");
Player PL = go. Addcomponent<player> (); Awake invoke right after this!
Pl. Getweapon (Weapongo);
...
In the above code, we will assign a value to the Handanchor when the player awake. If we put this action in start, then in Other.cs, when the getweapon is executed, handanchor is null reference.
Summary: We try to put other object reference settings and other things in awake processing. The assignment settings for these reference objects are then placed in start () to complete.
When Monobehavior has a definition [Executeineditmode]
When we define [Executeineditmode] for Monobehavior, we also need to be concerned about the execution of awake and start in the editor.
When the Monobehavior is assigned to Gameobject in the editor, Awake, Start will be executed.
When the play button is pressed and the game starts, awake, start will be executed.
When the play button is stopped, awake, start will be executed again.
When the package contains the Monobehavior scene in the editor, Awake, start will be executed.
It is important to note that this is not the way to set some temporary variable storage (private, protected). Because once we trigger Unity3d code compilation, the content stored by these variables will be cleared to the default value.
Let's take a look at the explanations in the unity Holy Scriptures.
Awake ()
Awake is called when a script instance is loaded.
Awake is used to initialize a variable or game state before the game starts. It is called only once throughout the lifetime of the script. Awake is called after all objects have been initialized, so you can safely talk to other objects or use things like
Gameobject.findwithtag such functions to search for them. The Awke on each game object are called in random order. Therefore, you should use awake to set up a reference between scripts and use start to pass information. Awake is always adjusted before start
Use. It cannot be used to execute a synergistic program.
Start ()
Start is called only before the update function is called the first time. Start is called only once in the life cycle of the behaviour. The difference between it and awake is that start is only called when the script instance is enabled.
You can adjust the deferred initialization code as needed. Awake is always executed before start. This allows you to coordinate the order of initialization.
For more highlights, please click http://www.gopedu.com/
Comparative analysis between Start () and Awake () in Unity3d scripts