My Start function is executing before the scene is done loading.

I have a persistent static GameControl object in my project. This object is responsible for saving and loading the game. Every scene has a LevelMaster object that reads from the persistent GameControl object (when loading savegames) and then does things in the level.

For instance, GameControl just loaded a save file. It says level 2 has two doors that the player left open, and the player just loaded level 2. So LevelMaster, in its start function, goes and opens whatever doors it read need opening, only two in this case.

The problem though is that LevelMaster is too fast. In this test case, it opens the first door, but then nullreferenceexceptions on the second door because that door’s collider hasn’t been loaded yet. The game complains stating I’m trying to do things on a non-existent collider, and I see the door is still shut.

Now, this problem was solved easily by me modifying execution order to delay LevelMaster a bit, but I want a more certain way of ensuring the level was loaded before executing LevelMaster’s start. OnLevelWasLoaded is no longer working it seems, and the other option I read about with the SceneManager involved something related to delegates and events that I didn’t understand.

If anyone has a solution to this, it would be great. Thanks.

PS: I also tried a coroutine that looks like this:

IEnumerator OnSceneLoad() {
		while (!currentScene.isLoaded) {
			Debug.Log ("Not loaded yet");
			yield return null;
		}
		LevelMasterInitLevel ();
	}

That didn’t work either, the debug statement didn’t even show and it still tried to modify the level’s objects too early, indicating that somehow Unity believes the level was loaded, yet not all the objects seem to “exist” fully yet.

I am not certain, but I want to try to get you a answer, so my guess is replace “iEnumeraor OnSceneLoad” with “void Start”