Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnEnable() vs Start()

Discussion in 'Editor & General Support' started by moproductions, Mar 30, 2012.

  1. moproductions

    moproductions

    Joined:
    Sep 28, 2010
    Posts:
    72
    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:

    Code (csharp):
    1. public static MCP thisInstance = null;
    2. void Awake()
    3.     {  
    4.         // make sure we only have one _MCP around
    5.         if( thisInstance != null  thisInstance != this )
    6.         {
    7.             DestroyImmediate(this.gameObject);
    8.             return;
    9.         }
    10.         thisInstance = this;
    11.         DontDestroyOnLoad(transform.gameObject);           
    12.     }
    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
     
    Last edited: Mar 30, 2012
  2. moproductions

    moproductions

    Joined:
    Sep 28, 2010
    Posts:
    72
    Solved by someone else:

    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.