Trouble with GameObject.Find() After LoadLevel.Load()
Hello everyone, I'm having trouble understanding why GameObject.Find() cannot find a gameObject after a Application.Load(). Essentially, after I load my level, it takes 1 additional frame before GameObject.Find() can find my object. As suggested by bdev in this thread: [http://forum.unity3d.com/threads/finding-objects-after-a-scene-load.106809/], I used two coroutines to call LoadLevel(), then wait a few frames before trying to Find() anything in them.
IEnumerator LoadLevel( string levelName )
{
Application.LoadLevel(levelName); yield return null; yield return null;
}
IEnumerator LoadLevelAndProcess(string levelName) {
yield return StartCoroutine( LoadLevel( levelName ) );// loads level waits 2 frames.
// insert find code here. everything *should* be ready at this point.
}
Unfortuantley, whether I use these extra coroutines or not, I still get the same result, the level loads, Find() won't work for 1 frame, then everything is fine.
#if DebugMaster
Debug.Log("TransToReal detected...");
#endif
while(GameObject.Find("3cRealLoadingScreenElements[enabled set1]") == null)
{
#if DebugMaster
Debug.Log("3cRealLoadingScreenElements[enabled set1] not yet found...");
#endif
yield return null;
}
realLoadingScreenHierarchy = GameObject.Find("3cRealLoadingScreenElements[enabled set1]").GetComponent<UIWidget>(); //should immediately exist...
Any ideas on why this happens is appreciated, thank you for reading.
Still having this problem in 2018 with the relatively newer Scene$$anonymous$$anager.LoadScene(...) under UnityEngine.Scene$$anonymous$$anagement. If I use a coroutine to wait a few frames after the call to Scene$$anonymous$$anager.LoadScene(...), the GameObjects I want are found by my tag, but that's after everyone else's Awakes and Starts were called, letting all hell break loose with the Null exceptions everywhere.. I'm seeing so much red T_T
Your answer
Follow this Question
Related Questions
Load Level script problem 2 Answers
Async Level loading issue. 0 Answers
GameObject.Find doesn't return elements of new scene 0 Answers
GameObject.Find throws be a error when trying to find a child. 1 Answer
How do I make a level code system? 3 Answers