EDIT: After doing some more research I came across this discussion, which explains what I described below…what a mess!
http://answers.unity3d.com/questions/217941/OnEnable-Awake-Start-order.html
Soooo I found out something interesting last night while trying to figure out why my LoadLevelAdditive() level’s gameObjects where calling their Awake() and Start() functions after everything in my current scene.
My class was as follow:
using UnityEngine;
using System.Collections;
public class ControlsManager : MonoBehaviour {
// Singleton access
private static ControlsManager _instance;
public static ControlsManager Instance
{
get{ return _instance; }
}
public string[] controlPresetScenes;
public GameObject[] controlPresets;
#region Properties
public GameObject ControlPreset01
{
get{ return controlPresets[0]; }
}
private GUI_Controls _guiControls;
public GUI_Controls GUIControls
{
get
{
if( !_guiControls )
_guiControls = GameObject.FindWithTag("Controls").GetComponent<GUI_Controls>();
return _guiControls;
}
}
#endregion
void Awake( )
{
if( !this.enabled ) return;
_instance = this;
Application.LoadLevelAdditive( controlPresetScenes[0] );
Debug.LogWarning("Loading HUD controls in " + this); // Gets called right away
}
void Start ( )
{
_guiControls = GameObject.FindWithTag("Controls").GetComponent<GUI_Controls>();
Debug.LogWarning("Storing HUD controls.", transform); // Gets call after FixedUpdate()/Update() from current scene's gameObjects
}
}
Whit the code above, the Debug text “Loading HUD controls…” in Awake() gets called right away, but the line in Start() to GetComponent<>() always returns null, and the Debug text “Storing HUD controls…” is getting called AFTER FixedUpdate() and Update() functions for objects in the current scene…couldn’t figure out why.
Then while trying ANYTHING i could think of, I added OnEnable() to the class:
using UnityEngine;
using System.Collections;
public class ControlsManager : MonoBehaviour {
// Singleton access
private static ControlsManager _instance;
public static ControlsManager Instance
{
get{ return _instance; }
}
public string[] controlPresetScenes;
public GameObject[] controlPresets;
#region Properties
public GameObject ControlPreset01
{
get{ return controlPresets[0]; }
}
private GUI_Controls _guiControls;
public GUI_Controls GUIControls
{
get
{
if( !_guiControls )
_guiControls = GameObject.FindWithTag("Controls").GetComponent<GUI_Controls>();
return _guiControls;
}
}
#endregion
// KEEP HERE OR GUI_CONTROLS WON'T EXECUTE RIGHT AWAY ???
void OnEnable()
{
// For some reason having OnEnable() defined makes the execution order of the loaded scene objects a priority
}
void Awake( )
{
if( !this.enabled ) return;
_instance = this;
Application.LoadLevelAdditive( controlPresetScenes[0] );
Debug.LogWarning("Loading HUD controls in " + this); // Still gets called right away
}
void Start ( )
{
_guiControls = GameObject.FindWithTag("Controls").GetComponent<GUI_Controls>();
Debug.LogWarning("Storing HUD controls.", transform); // Now gets called right after Awake() above !?!
}
}
…and BOOM! Like magic, the call to GetComponent<>() in Start() is now returning my GUI_Controls class, AND the Debug text “Storing HUD controls…” is now appearing before FixedUpdate() and Update() functions, right after the Debug text “Loading HUD controls…” in Awake() (which still gets called at the same time).
Is this normal? I was taken by surprise, for sure! If anyone can “enlighten” me I would really appreciate it lol…
Thanks for your time guys!
Stephane