- Home /
If a script is attached to more than one gameObject, will Start() & Awake() run more than once?
The same script. Different gameObjects disabled at runtime. Enabled at different time periods throughout the game.
Will Start() & Awake() run for each gameObject becoming enabled?
Or do I need to use OnEnable()?
Start and Awake will run "only once" for each script instance, OnEnable will run "every time" the script become enabled.
Answer by Bodrp · Apr 30, 2017 at 01:50 PM
Start()
and Awake()
will be called when the component becomes active for the first time only. They will act on their own instance of the component (since they are not static methods). OnEnable()
gets called everytime the component becomes active. By "acting on their own instance" I mean that for every instance of the component, they will be called and will have access to their instance's attributes, whatever may be their access level.
Awake()
will be called first, then OnEnable()
, then Start()
. If multiple components are activated at the same time, Awake()
and OnEnable()
methods will be called jointly for each instance. When that's done, each instance's Start()
method will be called.
For more information about the methods Unity calls with reflection on your components, you can refer to the execution order page of the manual.
A little correction on the order of Awake and OnEnable. They alternate. Each Awake is directly followed by its OnEnable
Answer by hexagonius · Apr 30, 2017 at 12:16 PM
Short answer is yes, both will run on each. AmEnable would be ran every time the script/ gameobject is toggled