OnEnable() vs Start() (39316)

Hello. I’m keeping a unique copy of an instance of an object called MCP that’s supposed to keep all the global variables around between scenes. I’m doing this with a combo of static variables and DontDestroyOnLoad() in my Awake() function:

public static MCP thisInstance = null;
void Awake() 
{	
	// make sure we only have one _MCP around
	if( thisInstance != null && thisInstance != this )
	{
		DestroyImmediate(this.gameObject);
		return;
	}
	thisInstance = this;
	DontDestroyOnLoad(transform.gameObject);			
}

I’m also doing some setup in OnEnable() so that other scripts can be sure that things are initialized by the time their Start() function is called. Things like setting up string arrays for player prefs, assigning delegate functions, etc. Now since this object is only being created once I assumed that OnEnable() and OnDisable() would only be called once as well. However, that isn’t the case. I’ve put print statements and sure enough it’s getting called every time. I’ve also put a check to make sure that it’s the same instance of the object’s OnEnable() getting called. I also put prints in Start() and noticed that that is only being called a single time. Is this expected behavior? I don’t want to put my initialization stuff in Start() because I can’t guarantee it’s set up on my first run-through. Is there another solution that I could use to make sure my inits in OnEnable() are being called once? Perhaps move them to Awake().
I’ve never tried to keep one instance of an object around throughout the game so all of this is new to me.
Thanks
-Mo

Awake() and Start() are guaranteed to be called only once per object. OnEnable(), however, will be called every time the object is enabled, either by another of your scripts or by Unity. Therefore, Awake() and Start() should be used for initialization purposes.

If you have initialization dependencies, you can use script execution order (accessed from the upper right of the inspector when the script is selected, or Edit > Project Settings > Script Execution Order). The execution order defines when a script’s Awake(), Start() and Update() are called relative to other scripts in a single frame.