I have read the unity scripting reference i understood the things said until i came across this part
Each GameObject’s Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth.
Can someone show me a good example of what this is saying?
Can’t depend on Awake() to do things in any kind of sequence.
Use it to initialize internal values in your script like dictionaries, lists, etc… which is useful because it always runs before Start and Update so you know they’ll be ready and initialized.
They are saying do not attempt to communicate with other scripts in Awake(), do that in Start().
An example of setting up a reference in Awake():
private Text textAttachedToThisGameObject;
void Awake()
{
//
// Get a reference to something attached to THIS game object
//
textAttachedToThisGameObject = GetComponent<Text>();
}
If you want to get a reference to another game object.
private GameObject someOtherGameObject;
void Awake()
{
//
// Get a reference to a different game object.
//
// Note: Find will not find inactive objects (ie if enabled == false)
//
someOtherGameObject = GameObject.Find("someRandomGameObject");
//
// DO NOT ASSUME anything about this object, it may or may not be initialised yet.
//
}
void Start()
{
//
// Use the reference to a different game object
//
someOtherGameObject.transform = doFunkyStuffToATransform(someOtherGameObject.transform);
}
MyScript myScript
Awake (){
// This line is fine
MyScript = GetComponent<MyScript>();
// This line might cause issues
myScript.someMember.SomeMethod();
}
Your second example is using GameObject.Find as an example but in the reference it states that Awake() is called after all gameObjects are initialized so there should be no problem there i am guessing.
As far as I understand the objects are all created/instantiated prior to Awake being called on any (though I can not find any documentation to support this) In practice this assumption has held true for me.
It’s actually a valid assumption. GameObjects in the scene will all be created before Awake is called. They will have references to their respective components. So Find and GetComponent are perfectly valid in Awake.
In short, you can get whatever references you like. But you should avoid using the references until Start.
This all becomes more complicated I you start calling Instantiate from Start or Awake.
Right, I do this myself but having just done a little research I could not find any documentation to support this (so I assumed I was just lucky in the instances where I do this). I did find some posts that claimed “OnEnabled and Awake() are called on objects when they are instantiated” or words to that effect, so then I started doubting my own assumptions (as per my corrections to my posts)…
There is one more thing about Awake() which is in my opinion super important and gave me a lot of headache: Awake() is always called regardless of the Gameobjects active state! Any code in Awake() will be called, even if the Gameobject itself is not active.
Are you sure on this? My results show Awake not being called on an inactive object until you activate it. Which is kind of annoying. Ideal behaviour would be calling Awake the moment an object is Instantiated. Like a constructor.
Oops I am sorry, I mixed something up BUT I am still not totally wrong, though.
Yes, Awake() will not be called if the holding GameObject is not active, but Awake() will be called on an Active GameObject if the Script is disabled.
Basically: GameObject active, Script disabled = Awake() will still be called.
This is all pretty simple once you actually start putting things into practice and can see what each method does and is supposed to be used for. It’s not something you should be overthinking.