UnityEvent - event listener does not catch event when the object is inactive

Hi there,
I have an object with attached game event listener script to it. It listens for a specific Unity event. In case however, the object is inactive (object.gameObject.SetActive(false)), it does not listen. Why is such behavior and is there a way to overcome it?
The only way I found so far is - I attached event listener scrip to another object (in my case GameManager), which is always active and then add another object scrip, which is executed even though the object is inactive. Still not quite sure if this is the right way of doing this.

1 Like

Inactive objects do not receive callbacks and events, this is by design. Set active true to listen callbacks and events, set it false to ignore all. Think of deactivation as temporary removal of that object from the scene. However, this is not implemented consistently. For example, the object still counts in childCount of it’s parent and there are special versions of GetComponent* what allows you to work with inactive objects, but no such args for events.

3 Likes

Thanks for the answer, @palex-nx - I would agree this is kind of inconsistent and in some case inconvenient I would say. In fact, there are cases, when the object can be inactive and you can have it subscribed for the event to make it active. Anyway - as long as I am aware about it and there is a workaround, I would just keep it in mind.

As far as I remember, you may choose activeSelft property from the event and set it to true, it should work for that particular case of activation by event.