Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

When is Start() called?

Discussion in 'Scripting' started by cbman, Jan 7, 2009.

  1. cbman

    cbman

    Joined:
    Oct 15, 2007
    Posts:
    23
    I have been tracking down a problem and it turns out Start() is called every time a scene is loaded. That would sound ok but in this case it's in a singleton class. The GameManager is created in the first scene and set up as singleton as outlined in other posts and the wiki.
    When a new scene is loaded that has a GameManager the Destroy is being called from Awake as expected. But everything indicates that Start is also called for this object because code in Start() is being run right after Destroy was called in Awake.
    And as if that's not enough, Update() is also called on this object.

    Is this correct behavior? How much is run in an object that had Destroy called on it in Awake() before it's actually destroyed?

    I was able to work around this with setting a static flag so the code in Start() is only run once. And because Update() is also run I had to use a flag in Update() to prevent code there from running since it depends on initialization done in Start() or it would crash.

    Some in-depth answer to this would be very welcome. I'm using C# and Unity iPhone.

    Thanks
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    My initial guess is that this is happening because Destroy() is not something that occurs immediately. From the Destroy entry in the Scripting Reference:

    Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

    So if you're calling Destroy on an object during Awake, you'll still get the rest of the rendering loop which involves Start (always happens after awake) and Update, but just that once. So it sounds to me like everything is in fact behaving properly.
     
  3. cbman

    cbman

    Joined:
    Oct 15, 2007
    Posts:
    23
    Thanks for getting the lightbulb turned on :) Now that I'm putting two and two together I can see what's going on. Learning new things all the time.
    With a new platform, new tool and new language it can be a bit confusing at times but it's getting better.
     
  4. Wyldfire

    Wyldfire

    Joined:
    Apr 5, 2006
    Posts:
    51
    I ran into this problem the other day too. My solution (in C# at least) is something like:

    if (this != instance) return;

    At the top of any functions I don't want running on the Destroyed object. It's being destroyed because it's an 'extra' copy of an already existing singleton, so this saves me from having to keep extra variables around to block the other functions.
     
  5. simone007

    simone007

    Joined:
    Oct 30, 2008
    Posts:
    221
    Nice tip, thanks
     
  6. cbman

    cbman

    Joined:
    Oct 15, 2007
    Posts:
    23
    That's an even better idea than the flag I use. Thanks for the tip!
     
unityunity