Unity Event not getting raised if gameobject is disabled

Why are they not raised if game objects are disabled? I don’t see it elaborated in the documentation Unity - Scripting API: UnityEvent

I’m sure UnityEvents are raised regardless of the object being active or not - the problem is that the code which triggers the event is probably not running due to the object being deactivated. Or do you have an example to the contrary?

1 Like

I have tested it on both cases if the game object is enabled or not. I can tell you right now, it’s not getting raised if the object is not active. This is similar to Coroutine. But I’m unsure why it’s not even raising an error or a warning.

This is a small piece of code concept that I used:

using UnityEngine;
using UnityEngine.Events;

public class TestUnityEvent : MonoBehaviour { // attach this to an empty game object
   public UnityEvent OnRaised;
   void Awake() {
       OnRaised.AddListener(DoSomething);
       OnRaised.Invoke();
   }
   void DoSomething() => Debug.Log("Raised!"); // "should" be raised regardless of gameobject's enable state
}

What I intend to do was to set the game object active when OnRaised event is raised but I can’t do that if it won’t get raised. For now, I had to add a line to my managed code which I shouldn’t do but it works for now. I do hope this gets fixed or at least give us some logging information that it shouldn’t work

Awake is not called until the object is activated as described in the documentation:

So, it’s not the event, it’s Awake.

3 Likes