- Home /
does finction start or awake run when the object or script is enabled mid game
i want to knwo if function Start and/or function awake when the script is enabled mid game from an other script?
Answer by Bunny83 · Jan 02, 2013 at 01:13 AM
Yes, they are called mid game. Basically there are some different situations:
1 The game object is active but the script is disabled.
2 The game object is deactivated but the script is enabled.
3 The game object is deactivated and the script is disabled.
4 The game object is active and the script is enabled.
In general there's no difference between the behaviour of objects in a scene and those created at runtime with Instantiate.
In case 1 when the object is created only Awake is called immediately when the object is created. OnEnable and Start will be called when you enable the script.
In case 2 when the object is created nothing is called. When you set the game object active, Awake will be called followed by OnEnable and Start
In case 3 when the object is created nothing is called. When you set the game object active, only Awake will be called. If you enable the script later OnEnable and Start will be called.
In case 4 when the object is created Awake will be called followed by OnEnable and Start.
Keep in mind that all Start calls have a fix position in the game loop. The Start call is always delayed until the next frame. OnEnable and Awake will be called immediately when appropriate. That means in case 4 when you call Instantiate to create an object in game, Awake and OnEnable will be called before the Instantiate call returns.
edit
Just in case that fact isn't cleat:
Awake and Start is called only once in the lifetime of an object. So once it has been activated / enabled once Start and Awake won't be called again. OnEnable (and OnDisable) will be called each time it is enabled / disabled.
BTW OnEnable and OnDisable are linked with the active state of the game object as well. So OnDisable will be called when you either deactivate the GameObject or when you disable the script.
So OnEnable will be called when this condition becomes true:
(gameObject.active && enabled)
OnDisable will be called when the condition becomes false.
Answer by spritesobhan · Apr 30, 2017 at 10:19 AM
So Awake is called when GameObject is Active, even if Script is disabled. Start is called only when both are Active.
Answer by ballz · Feb 02, 2018 at 03:27 AM
when do MonoBehaviour callbacks
A = awake() E = OnEnable() S = Start()
run with respect to active & enabled states?
class created activate enabled
active : enabled A->E->S
active : disabled A E ->S
deactivated:enabled nothing A->E->S
deactivated:disabled nothing A E->S
NB:
Enable / Disable are called many.
A & S are called only once in lifecycle.
A & E calls will interleave in enabled classes
execution order of scripts can be assigned in Edit | Project Settings
line 7 should be like this:
active : disabled A - E -> S
because game object is already activated and its script that it's disabled so you can enable it.