I have a MonoBehaviour persistent between levels (DontDestroyOnLoad). Each time a new level is loaded I need to call some code on it. I would use OnEnable or OnLevelWasLoaded but I also need to make sure the code is called after all Awake calls. So, how can I call some code on a persistent MonoBehaviour after all Awake calls each time a new level is loaded?
My current solution is the following:
private bool _lateEnable = false;
void Awake() {
// ...
DontDestroyOnLoad(this.gameObject);
// ...
}
void OnEnable() {
_lateEnable = true;
// ...
}
void Update() {
if(_lateEnable){
OnLateEnable();
_lateEnable = false;
}
// ...
}
void OnLateEnable() {
// Code called after all Awakes
}