OnEnable() is not called when SetActive(true)

Hey!
I want to make a simple jump and run game where different sets of obstacles are generated again and again so that it never ends.

I tried to do this by activating and deactivating the objects with the SetActive() method (and then changing their position when they are activated again). The problem is, that OnEnable() isn’t called when the object is activated.

Does anyone know why this happens or another way to do this?

Thank you!!!

Since the script itself wasn’t turned off OnEnable() wouldn’t be called, if you really want the script to be re-enabled, deactivate it with script.enabled = false; This will also trigger the OnDisabled() method, then just reenable it after you turned the gameobject back on with script.enabled = true; This will run OnEnable() again.

I ran into this exact same problem but for a different reason. The script that I was expecting OnEnable to fire from each time the GameObject was SetActive(true) wasn’t attached to that GameObject but to another GameObject. So in that case you do have to disable the Script when you’re done using it and then reenable it again when you need it so that OnEnable will fire.

These methods will be called when the attached GameObject is toggled. So GameObject.SetActive(false) will trigger OnDisable() on any attached script. Same goes with hierarchy. When Parent GameObject is toggled everything in hierarchy have calls (OnDisable, OnEnable). However, the enabled property of the MonoBehaviour scripts on that GameObject will still return true. Branch is OFF, single scripts stays ON/OFF → no matter in what state branch is.