Search Unity

Unity Event not getting raised if gameobject is disabled

Discussion in 'Scripting' started by CraftedGaming, Feb 20, 2021.

  1. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    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?
     
    Madgvox likes this.
  3. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class TestUnityEvent : MonoBehaviour { // attach this to an empty game object
    5.    public UnityEvent OnRaised;
    6.    void Awake() {
    7.        OnRaised.AddListener(DoSomething);
    8.        OnRaised.Invoke();
    9.    }
    10.    void DoSomething() => Debug.Log("Raised!"); // "should" be raised regardless of gameobject's enable state
    11. }
    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
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    CraftedGaming and Madgvox like this.