Renamed from: Null but not null (equality override problem)?
I am dealing with a problem I just can't get to the bottom of. Actually a co-worker of mine (oddly enough) is dealing with the exact same issue on a totally unrelated area.
GameObject sceneRoot = GameObject.Find(levelName);
Debug.Log(sceneRoot + " "+(sceneRoot == null));
The output that this produces is
UnityEngine.GameObject True
How can this be? We have been researching this problem all day. In my case the object named "levelName" has been added to the scene graph via additive scene loading. I can see it on the scene graph as it gets added. The stack trace suggests the object is there and the null is coming from the inside of the game object. What would cause this odd behavior? Do these objects not get really initialized until the end of the frame or something?
Additional note: Trying to call GetComponent on this yields:
NullReferenceException
UnityEngine.GameObject.GetComponent[Transform] () [0x00000]
I get the NRE, but it's like its coming from withn GetComponent... my code should be at the top of the stack, not GameObject's.
Edit(8/5): This looks like the default behavior of Find upon more research. This is very strange to me:
GameObject go = null;
Debug.Log("Object "+go + " " + (go == null));
go = GameObject.Find("TestScene2");
Debug.Log("Object " + go + " " + (go == null));
Yields this:
Object True
Object UnityEngine.GameObject True
So I can print it but it's null?
Edit(8/5 ... later in the day):
int frame = 1;
void Update()
{
if (frame == 1)
{
Application.LoadLevelAdditive("TestScene2");
}
GameObject go = GameObject.Find("AnObjectInTestScene2");
UnityEngine.Debug.Log(frame++ +" "+go + " " + (go == null));
}
Yields this:
1 UnityEngine.GameObject True
2 UnityEngine.GameObject False
...
So this tells me I cannot call Application.LoadLevelAdditive and use something in the scene graph immediately. Find only works on the frame after the load?
Workaround: After some more research and the comment from SpikeX I decided to use the Async version of LoadLevelAdditive, this way I am not relying on wall time to accomplish this. I just had to add some more complexity and gears to deal with the async. This would be a nice thing to get documented in the docs. The explicit presence of LoadLevelAdditiveAsync makes one believe that LoadLevelAdditive is truly sync, but it's truly not.