Search Unity

Initiation callback for inactive GOs?

Discussion in 'Scripting' started by Dom3D, Mar 9, 2014.

  1. Dom3D

    Dom3D

    Joined:
    Dec 19, 2012
    Posts:
    21
    I am looking for an Awake-like method that also runs on inactive GOs as soon as it's are added to the scene. The docs says that it calls Awake when the script get's loaded but it actually does it only when the GO get's activated. One could also think of an "OnCreate" as counterpart to the existing OnDestroy.

    What I want to achieve is having components that are able register themselves into an custom event stream even if the owner is inactive.
     
  2. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Well... if the object is not self aware that it is active, the only way to do this is to add it in a list of deactivated GO and call a static method that will trigger the registration to the event.

    Be aware that if you call static methods on deactivated GO you will not have the possibility to rely on the Unity lifecycle calls on that object until it will not be enabled.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Are you positive that this is the case? My experience has been that Awake always runs but Start will only run once - either when the object is created or when the object is activated (in cases when it is instantiated as inactive or already in the scene).

    In either case, you're probably looking for OnEnable. It runs every time an object is activated (and also when it is instantiated if it is active when it is instantiated).
     
  4. jonathan.hockman

    jonathan.hockman

    Joined:
    Sep 17, 2013
    Posts:
    10
    Just another thought. You could also just disable the objects you want disabled within their Awake function, so you would have them enabled to begin with and maybe use a public boolean that you set in the editor and in the Awake function check to see if the boolean is true, and, if so, disable the Game Object.