GameObject.Find() returns null even if the gameObject I'm looking for is active
Hello,
I'm doing an asteroid-like game. I found an unfortunate problem. I would like to write this on one of my scripts:
void Awake()
{
// Get game settings
gameSettings = GetComponent<GameSettings>();
}
void Start() {
// Get the asteroid containers
asteroidContainer = GameObject.Find("Asteroids"); // GameObject.Find() returns null here
}
But only this is working:
void Awake()
{
// Get game settings
gameSettings = GetComponent<GameSettings>();
// Get the asteroid containers
asteroidContainer = GameObject.Find("Asteroids"); // This works
}
This does not road-block me, but I always thought that Awake() was used to setup the actual gameobject and Start() to setup "links" to other external (not the actual gameobject) resources.
I will attach an image of Unity, if it can help:
Answer by Giovarco · Oct 20, 2017 at 12:54 PM
Even if I'm not sure how to solve it, I found the problem!
GameObject.Find() works correctly. The fact is that one of public functions of my script Factory.cs is used before Factory Start() is called. I will explain it: I have another script called LevelMaker.cs which generates the level for me and this generation is made inside LevelMaker Start(), but It seems that while I generate the level in LevelMaker Start(), Factory Start() is still not called. This is why it works if I move the code to Factory Awake(). The fact now is: do you think that this behaviour is nice? For me, this should be addressed somehow because it could be confusing.