# UnityEvent.Invoke error

**URL:** https://discussions.unity.com/t/unityevent-invoke-error/161946
**Category:** Questions & Answers
**Created:** [March 30, 2016, 10:02am UTC](https://discussions.unity.com/t/unityevent-invoke-error/161946 "2016-03-30T10:02:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sneakki](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sneakki](https://discussions.unity.com/u/Sneakki)
#### Post date: [March 30, 2016, 10:02am UTC](https://discussions.unity.com/t/unityevent-invoke-error/161946/1 "2016-03-30T10:02:01Z")

</div>

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](https://www.youtube.com/watch?v=0AqG1fDhPT8)

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 ☹

---

<div class="post-metadata">

### Author: ![adprasad89](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@adprasad89](https://discussions.unity.com/u/adprasad89)
#### Post date: [December 21, 2017, 4:25am UTC](https://discussions.unity.com/t/unityevent-invoke-error/161946/2 "2017-12-21T04:25:38Z")

</div>

@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 🙂
