Invoke returning a null reference exception,

Hey hi hello, sorry if this is really obvious but I’m new to unity. Basically I’m trying to invoke a unity event, but when it’s run it gives me a null reference exception on the Invoke line. I tried having the invoke method only run if it wasn’t null, but it still tries to run and still gives me the null reference exception. What should I do?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class DialogueTriggerManual : MonoBehaviour
{
    public UnityEvent speak;

    float bufferTime = 3;
    private bool inRange = false;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    //If object with player tag comes in contact with the object, set variable true
    public void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            inRange = true;
        }
    }

    public void OnTriggerExit2D(Collider2D collider)
    {
        if(collider.gameObject.tag == "Player")
        {
            inRange = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!GameManager.playerInCutscene && Input.GetKey(GameManager.interact) && inRange && bufferTime == 3)
        {
            bufferTime = 1;
            speak.Invoke();
        }

        if(!GameManager.playerInCutscene && bufferTime == 1)
        {
            StartCoroutine(Buffer());
        }
    }

    //So the the game register a click out as a click in
    public IEnumerator Buffer()
    {
        for(int i = 1; i <= 3; i++)
        {
            yield return new WaitForSeconds(0.1f);
            bufferTime = i;
        }
    }
}

201945-37999f57-62df-473a-bfde-e80d8a52585a.png

To start with, this may be your first question on this site but well done for the info , the script and the image. You’ve described the problem well. I just wish that other first-timers would put in as much effort as you did. Top job.

The event definition looks fine. We can pretty much assume the target script BlueDialogue exists, as does the method you are calling because, if you had deleted either of those, Unity would have given you an error message in the Inspector event definition. Even if you had destroyed NPC1 in script, you wouldn’t get a Null Reference.

OK. On to your problem. You do not need to check a UnityEvent for null before invoking it. This is different from C# events when you must check for null so don’t worry about that part. My first thought was that you were trying to invoke the event on a prefab which has not yet been instantiated but I tested that and it seems that a UnityEvent can run against prefabs so that’s not the problem.

So, I created your project as closely as I could using all your code and it worked fine. Are you certain the the Null Reference was actually on the Invoke statement? Perhaps post the script for BlueDialogue as well (or just part of it, if large).

If diagnosing your problem I’d check to see if NPC1 was null before the Invoke. That means using GameObject.Find(“NPC1”) and checking the result but I still think UnityEvent manages not to throw a Null Reference.