Awake -> OnEnable -> Start, not good

Hi

I just discovered the order of execution is Awake->OnEnable->Start. Wouldn’t it be better if it’s Awake->Start->OnEnable?

Because I’m enabling/disabling at runtime, I need a hook for this event. Now I can’t use OnEnable because it’s called at Awake stage, and the script references are not setup properly. If it’s called after Start, then everything’s cool.

Maybe you could add a variable to see if the GO is initialized and then check the variable in OnEnable?

eg.

void OnEnable()
{

    // Only DoStuff is the GO has been initialized
   if(initialized) DoStuff();
}

void Start()
{
   initialized = true;

   // Run DoStuff here because it won't be run in the first OnEnable
   DoStuff();
}

I could, but it’s messy because I have subclasses of it all over using the OnEnable override. I mean, if Awake is meant to be for setup, and Start is the actual initialization, then I would see OnEnable be called after Start, not before.

thats due to how you define awake and start.
I would look at it more like:

Awake: To initialize and setup this game object
Start: To initialize and setup things that are related to other game objects.

in the light of this OnEnable before start makes a lot of sense as its related to this object only, as it is the “comes into view” event handling basically.

question though is what you have in onenable thats a problem in this context cause it shouldn’t do initialization and processing intense things in there due to when onenable gets called nor should it have any relationships to other things other than its own childs as the enable and disable could happen at any time and are meant to handle just this objects things.

Ok, what I’m doing is a memory pool of GameObjects, so they are reused by disable/enabling. I’m resetting data values in OnEnable since Start is only called once. Of course this is different from what GameObject OnEnable is designed for, since Unity expects Gameobjects to be be destroyed and recreated destroyed using Instantiate. but in practice, this won’t work for more complicated stuff due to garbage collection.

0

1 Like

What came first chicken or the egg?
Its just a name is it not?
Anything you do in one i imagine you can do in the other.

http://forum.unity3d.com/threads/95794-PoolManager-is-now-available-in-the-Asset-Store!
http://vonlehecreative.wordpress.com/2010/01/06/unity-resource-gameobjectpool/
http://forum.unity3d.com/threads/34582-Stop-wasting-memory-recycle-your-objects!