Search Unity

Active state, calling Awake, everything is too late since 4.0f7

Discussion in 'Scripting' started by Asse83, Dec 4, 2012.

  1. Asse83

    Asse83

    Joined:
    Aug 1, 2009
    Posts:
    169
    We've just updated Unity3D from 3.5.6f4 to 4.0f7 and since that, many states and calls seem to occur later as we were used to.

    E.g. when you instantiate a GameObject, Awake() seem to get called at LateUpdate() and not at the time the object gets instantiated. When I call SetActive(true) on a GameObject the state is still false within my method.

    Is this really the intended behavior?
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    This behavior is correct, quote from documentation:

    "Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine."

    So Awake is not called directly after each instance of an object is created, but after all are instantiated. As far as i know(remember), up until they added the script execution order and your ability to dictate that, this was common and many things were used(singletons, etc) to control how and when something was instantiated in a particular scene. This shouldn't be that painful now.
     
    Last edited: Dec 4, 2012
    grobonom likes this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    That's right. I use Awake() to set up self's references ie script = this; and Start to chat to other scripts and do the general setup.
     
    grobonom likes this.