Search Unity

Avoiding a race condition with instantiate?

Discussion in 'Scripting' started by Nanako, Mar 7, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I have a n object which does stuff when it's created, in Start()
    I would like it to sometimes (not always) not do that stuff, when instantiated from a certain area of code.


    So my question is, in the part of code where i'm instantiating the object, can i reliably set a variable to some value on the next line, to tell it not to do its usual startup procedure?

    Does the object call Start() immediately when its created, or when the current code finishes, or the next frame, or what?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Start is called before the first frame of updates.

    In practical terms this means you can rely in any data set in the lines of code immediately following Instantiate.

    Awake has the opposite behaviour, and will run as part of Instantiate.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I found an awesome image that explains it all. If you google for unitylifetime.png you will see all the different variants that people have made, including a flowchart-y one and a circular one. It clarified a lot for me.
     
    Nanako likes this.
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
  5. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
  6. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    Yep, use Awake for anything you need to have absolutely immediate access to...