Hi, I have a gameobject which is turned off by default. This gameobject has a script attached which is enabled by default, but won’t obviously execute because it’s parent gameobject is disabled (right?). I also have a manager class, which turns on this gameobject when necessary, and I was expecting the script to then automatically run as it is enabled and the object it’s attached to was been enabled. To test I put a debug.log message in the update function of the script, and also a debug.log message within an OnEnable function - assuming that when the gameobject is activated, therefore the script is, therefore OnEnabled() would be called. But I’m not getting any messages out, so I’m wondering whether it’s jsut a bug, or where I have a fundamental misunderstanding of the how this works. Thanks in advance for any assistance.
If you have an object that is disabled by default AND no other object has a reference of it, then you won’t be able to find it at all.
The way to do this is by linking the disabled object instance to the instance that’s going to activate it later:
public class WillEnableGOTest : MonoBehaviour
public GameObject objectToEnable;
// Collider col = objectToEnable.GetComponent<Collider>();
// do other stuff
then you link the objects on the inspector
Perhaps, you could attach the script to an empty and then child you gameobject to the empty. Then you would be able to disable the gameobject without disabling the script. Either way it’s hard to tell without the specifics.