Hello,
I have a class called Insect, which on Awake, does the following:
protected override void Awake()
{
base.Awake();
spider = GameObject.FindGameObjectWithTag("Spider").GetComponent<Spider>();
}
The Spider is our player character, is tagged “Spider” … and is always placed in the scene hierarchy.
99% of our Insect Population has no problem finding that GameObject via FindGameObjectWithTag on Awake. But, in one level, with a certain group of insects, I’m getting NullReferenceExceptions.
Why would FindGameObjectWithTag ever fail? The Spider GameObject is placed in the world and properly tagged.
Further investigation revealed that the Insects complaining were all children of the same empty GameObject. If I moved the three children up the hierarchy one level… the NullReferenceExceptions go away.
So… order of execution is determined … randomly? I don’t understand why being parented to another object could possibly result in Awake getting called at a different moment in the application’s execution. I realize I’m assuming that for some reason the Insect is getting its Awake call earlier in the execution cycle in this case… but perhaps that’s not the case and there’s some other reason FindGameObjectWithTag fails.
Any assistance would be greatly appreciated.
Cheers,
David