Home > Software engineering >  Only one Event Action is null
Only one Event Action is null

Time:01-01

I am trying to understand Events in C# (Unity), but this issue drives me crazy a little bit.

I have an Event script the working part of which is

public class GameEvents : MonoBehaviour
{
    public static GameEvents current;

    private void Awake()
    {
        current = this;
    }

    public event Action<string, string, GameObject, int, bool> onDoorTriggerEnterEvent;
    public event Action<string, string, GameObject, int, bool> onDoorTriggerExitEvent;
    public event Action<GameObject, int, int> onDoorOpenSequenceEvent;

    public string triggerEnteredName { get; private set; }
    public string triggerEnteredType { get; private set; }
    public GameObject doorWingOldTransform { get; private set; }
    public int doorAccessLevel { get; private set; }
    public int playerLevelPoints { get; private set; }
    public bool triggerEntered { get; private set; }
    public string doorTypeToOpen { get; private set; }
    public string doorNameToOpen { get; private set; }
    public GameObject doorWingOldTransformToOpen { get; private set; }

    public void DoorOpenSequence(GameObject doorWingOldTransformToOpen, int doorAccessLevel, int playerLevelPoints)
    {
       if (onDoorOpenSequenceEvent != null)
         {
            this.doorWingOldTransformToOpen = doorWingOldTransformToOpen;
            this.doorAccessLevel = doorAccessLevel;
            this.playerLevelPoints = playerLevelPoints;

            Debug.Log(doorWingOldTransformToOpen);
            Debug.Log(doorAccessLevel);
            Debug.Log(playerLevelPoints);

            onDoorOpenSequenceEvent(doorWingOldTransformToOpen, doorAccessLevel, playerLevelPoints);
        }
        else
        {
            Debug.Log("Event is null");
        }
    }

    public void DoorTriggerEnter(string triggerEnteredType, string triggerEnteredName, GameObject doorWingOldTransform, int doorAccessLevel)
    {
        if (onDoorTriggerEnterEvent != null)
        {
            this.triggerEnteredType = triggerEnteredType;
            this.triggerEnteredName = triggerEnteredName;
            this.doorAccessLevel = doorAccessLevel;
            this.doorWingOldTransform = doorWingOldTransform;
            triggerEntered = true;
            onDoorTriggerEnterEvent(triggerEnteredType, triggerEnteredName, doorWingOldTransform, doorAccessLevel, triggerEntered);
        }
    }

    public void DoorTriggerExit(string triggerEnteredType, string triggerEnteredName, GameObject doorWingOldTransform, int doorAccessLevel)
    {
        if (onDoorTriggerExitEvent != null)
        {
            this.triggerEnteredType = triggerEnteredType;
            this.triggerEnteredName = triggerEnteredName;
            this.doorWingOldTransform = doorWingOldTransform;

            triggerEntered = false;

            onDoorTriggerExitEvent(triggerEnteredType, triggerEnteredName, doorWingOldTransform, doorAccessLevel, triggerEntered);
        }
    }
}

The first two events is working perfectly. But the third one gives me a NULL reference Exception in this line:

onDoorOpenSequenceEvent(doorWingOldTransformToOpen, doorAccessLevel, playerLevelPoints);

These Debug.Logs gives me an information that all data received, but event is still NULL (I mean when I try it withoud if(onDoorOpenSequenceEvent != null)).

The only difference between these events calling from outside is that first two of them called from OnTriggerEnter, and the third one from void Update through Input.GetKeyDown, but it is no matter from where I send data because I've tried all the ways and that event is still NULL.

I understand that this code looks not very good but I am new to C#.

Please, help me with this. Thanks.

CodePudding user response:

I can't see your code anywhere else. Therefore, I guess you didn't assign onDoorOpenSequenceEvent somewhere else, like this

onDoorOpenSequenceEvent  = somefunction;

CodePudding user response:

The question is not where these events are invoked but rather who/what is subscribing to them. If you never attached any callback/listener to an event ( =) then it will be null

So actually this is nothing to worry about, it just means that currently nobody cares about this event and even if you would (not possible due to the null ref exception of course) invoke it nobody listens and reacts to it.

Just remove your else block ;)


As a side note since you have multiple parameters I would suggest you rather use a proper delegate in order to properly name your parameters like e.g.

public delegate void TriggerEvent(string triggerType, string triggerName, GameObject oldTransform, int doorAccessLevel);
public delegate void SequenceEvent(GameObject doorWingOldTransformToOpen, int accessLevel, int playerPoints);

public event TriggerEvent onDoorTriggerEnterEvent;
public event TriggerEvent onDoorTriggerExitEvent;
public event SequenceEvent onDoorOpenSequenceEvent;
  • Related