OnLevelWasLoaded after Awake (Unity 5.3)

OnLevelWasLoaded isn’t called In the Editor at start of a single scene (simple scene with camera, direction light and game object with example script). But it’s called after Awake on MacOS and Android build (other OSs wasn’t tested).

Example script:

using UnityEngine;

public class TEST : MonoBehaviour {

    public void Awake()
    {
        Debug.LogError("Awake");
    }

    public void Start()
    {
        Debug.LogError("Start");
    }

    public void OnLevelWasLoaded(int level)
    {
        Debug.LogError("OnLevelWasLoaded");
    }
}

Result in the Editor:

Awake

Start

Result in the game:

Awake

OnLevelWasLoaded

Start

Is it normal behavior? Version: 5.3.0f4 Personal.

PS: sorry for my english.

If i remember correctly OnLevelWasLoaded is called if a scene is loaded when you load an additional/an other scene with LoadLevel, LoadLevelAsync, LoadLevelAdditive, …

and I think unity loads your main scene like this after the unity logo, but the editor starts the desired scene directly. so I’d say yes this is normal behavoir

This behavior is different in Unity 5.1.

In Unity 5.1, OnLevelWasLoaded is not called when the first level is loaded. Unity 5.1 doesn’t consider the first level is a new one, so the method is called, for example, when you call SceneManager.LoadSceneor Application.LoadLevel once the game is running.

Unity 5.3 works different, and I don’t know if it’s a bug or not (more info here). With this version, OnLevelWasLoaded is called after Awake, but only in game. In the editor, OnLevelWasLoaded works as in Unity 5.1.

Pretty strange :slight_smile:

OK so I should have previously specified that I am currently using windows Version 5.3.0f4 Personal.
I copied exactly what you have and behaved exactly how you said which I believe from the documentation given by unity is the correct.

Then I decided to change the code a little so it checks to see if there is already a GameObject with this code. If so delete it-self otherwise don’t Never destroy this GameObject.

 public class AwakeTest : MonoBehaviour {
 
	public static AwakeTest control;
     public void Awake()
     {
         Debug.LogError("Awake");
		 if(control == null)
		{
			DontDestroyOnLoad(gameObject);
			control = this;
		}
		else if(control != this)
		{
			Destroy(gameObject);
		}
     }
 
     public void Start()
     {
         Debug.LogError("Start");
     }
 
     public void OnLevelWasLoaded(int level)
     {
         Debug.LogError("OnLevelWasLoaded");
     }
 }

And also made a button to reload the scene:

public class ButtonScene : MonoBehaviour {

	void OnGUI(){
		if (GUI.Button(new Rect((Screen.width/2)-50, (Screen.height/2)-15, 100, 30), "Reload"))
			SceneManager.LoadScene("Main");
	}
}

Result in the log:

//started the game
Awake
Start
//clicked the button
Awake
OnLevelWasLoaded
OnLevelWasLoaded

So with this it would seem that OnLevelWasLoaded is executed before the GameObject is Destroyed or is queued before Awake is done processing. If this could be confirmed or denied it would be greatly appreciated.