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;
}
}
}