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 ![]()
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?
– Theacesofspades