UnityEvent.Invoke error

I am trying to reproduce the Unity event system from the Live session tutorial Live Training April 13th, 2015: Creating a Simple Messaging System - YouTube

and the thing is that thisEvent.Invoke() gives an error described as this:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Events.BaseInvokableCall.AllowInvoke (System.Delegate delegate) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:108)

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

public class EventManager : MonoBehaviour {

	private Dictionary <string , UnityEvent> EventDictionary;

	private static EventManager eventManager;

	private static EventManager instance 
	{
		get
		{
			if (!eventManager) {
				eventManager = FindObjectOfType (typeof(EventManager))as EventManager;

				if (!eventManager) {

					Debug.Log ("No event manager(script) in scene");
				}
			} else {
				eventManager.Init ();
			}
			return eventManager;
		}
	}

	void Awake(){
	//	eventManager = GameObject.Find ("ScriptHolder").GetComponent<EventManager>();
	//	eventManager.EventDictionary = new  Dictionary<string,UnityEvent>();
	}

	void Init (){

		if(EventDictionary == null) EventDictionary = new Dictionary<string,UnityEvent>();
	}


	public static void StartListening(string eventName, UnityAction listener) {
		//Pre: name of event and function pointer 

		    UnityEvent thisEvent = null;
		if (instance.EventDictionary.TryGetValue (eventName, out thisEvent)) {
			//tryGetValue (more efficient than GetValue+CatchException)
		
			thisEvent.AddListener (listener);

			} 
		else {
			//if doesn't exist create a new entry 
			thisEvent = new UnityEvent ();
			thisEvent.AddListener (listener);
			Debug.Log ("adds listener:" + eventName);
			instance.EventDictionary.Add (eventName, thisEvent);
		
			}
    //funcEnd
	}

	public static void StopListening(string eventName, UnityAction listener){
	
		if (eventManager == null)
			return;

		UnityEvent thisEvent = null;

		if (instance.EventDictionary.TryGetValue (eventName, out thisEvent)) {
			//tryGetValue (more efficient than GetValue+CatchException)

			thisEvent.RemoveListener (listener);

		} 
	//funcEnd		
	}

	public static void TriggerEvent(string eventName){
	
		UnityEvent thisEvent = null;
		if (instance.EventDictionary.TryGetValue (eventName, out thisEvent)) {
			//tryGetValue (more efficient than GetValue+CatchException)
			//if(thisEvent==null)Debug.Log("doesn'tFinnd");
			thisEvent.Invoke ();

		} 
	
	}

}

which basically should be a copy to the one in the video and also i have addListener and respond to input call here

    	if (Input.GetMouseButtonDown (1)) {
    			GameObject.Find ("Character").GetComponent<MoveCurveY> ().enabled = true;
    			EventManager.TriggerEvent ("jump");
    		}
    
  //  those are in different classes 

void OnEnable(){
		EventManager.StartListening("jump",jumpListener);
	}

No idea how to fix this :frowning:

It gives me an error saying: The Type or namespace name "GunType" could not be found. Are you missing a using directive or an assembly reference?

–

1 Answer

1

@Sneakki Not sure if you need an answer anymore but what I found out when I had this error was my listener was null when it was added. So even though the event was not null the “invoke” was being called on a null action/listener. I changed my OnEnable to check if the listener was null and set it if it was.

UnityAction scoreListener;

void OnEnable()
{
    if( scoreListener == null)
    {
        scoreListener = new UnityAction(Score);
    }
    EventManager.Instance.StartListening("score", scoreListener);
}

void OnDisable()
{
        EventManager.Instance.StopListening("score", scoreListener);
}

Hope this helps someone :slight_smile:

So, it sounds like you DID define the enum inside your GunScript. In that case you will need to refer to it's type as "GunScript.GunType" myTarget.gunType = (GunScript.GunType)EditorGUILayout.EnumPopup(myTarget.gunType); You MAY even need to cast to that type type when passing IN the target's gunType. myTarget.gunType = (GunScript.GunType)EditorGUILayout.EnumPopup((GunScript.GunType)myTarget.gunType); Alternatively, you can define the enum OUTSIDE the class.

–