[SOLVED] UnityEvents - how to pass a variable from trigger to listener function?

Hi all,
I just completed the Unity Event System live training tutorial over here: http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/events-creating-simple-messaging-system

I’m trying to decouple my scripts using the above system, but I can’t figure out how to send a variable from the EventManager.TriggerEvent() to my listener. I’m using the exact same EventManager code as the tutorial, but I’ll paste it here for easy reference.

Basically I’m trying to call my GUI script to update the Player’s coin amount via the event system. I’ve added comments on the lines that needs fixing.

EventManager

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

public class EventManager : MonoBehaviour {

    private Dictionary <string, UnityEvent> eventDictionary;

    private static EventManager eventManager;

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

                if (!eventManager)
                {
                    Debug.LogError ("There needs to be one active EventManger script on a GameObject in your scene.");
                }
                else
                {
                    eventManager.Init ();
                }
            }

            return eventManager;
        }
    }

    void Init ()
    {
        if (eventDictionary == null)
        {
            eventDictionary = new Dictionary<string, UnityEvent>();
        }
    }

    public static void StartListening (string eventName, UnityAction listener)
    {
        UnityEvent thisEvent = null;
        if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.AddListener (listener);
        }
        else
        {
            thisEvent = new UnityEvent ();
            thisEvent.AddListener (listener);
            instance.eventDictionary.Add (eventName, thisEvent);
        }
    }

    public static void StopListening (string eventName, UnityAction listener)
    {
        if (eventManager == null) return;
        UnityEvent thisEvent = null;
        if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.RemoveListener (listener);
        }
    }

    public static void TriggerEvent (string eventName)
    {
        UnityEvent thisEvent = null;
        if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.Invoke ();
        }
    }
}

EventTriggerTest

using UnityEngine;
using System.Collections;

public class EventTriggerTest : MonoBehaviour {
    void Update () {
        if (Input.GetKeyDown ("q"))
        {
            EventManager.TriggerEvent ("coinUpdate");   // How do I pass the coin amount from here?
        }
    }
}

EventTest

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

public class EventTest : MonoBehaviour {

    private UnityAction coinUpdateListener;

    void Awake ()
    {
        coinUpdateListener = new UnityAction (UpdateCoinAmount);   // This line needs fixing?
    }

    void OnEnable ()
    {
        EventManager.StartListening ("coinUpdate", coinUpdateListener);
    }

    void OnDisable ()
    {
        EventManager.StopListening ("coinUpdate", coinUpdateListener);
    }

    void UpdateCoinAmount(int amount)
    {
        Debug.Log ("Update amount " + amount);
    }
}

Thanks!

Solved! I just overloaded the StartListening and StopListening functions to take in this type:

I didn’t understand how you solved it. May you explain? :slight_smile: @pixpusher2

I didn’t understand how you solved it. May you explain?

That original post was from almost 2 years ago, @indiedev37 .
He solved it by changing the argument parameter type to a UnityEvent with 1 parameter (check the link he posted to that).

thanks i figured it out with an override :smile:
public class UnityEvent_Int : UnityEvent { };

@methos5k I know it is an old thread, but could You please show how the classes should look like after changing the UnityEvent?

@applicattura

using UnityEngine.Events;

[System.Serializable]
public class myInheritedEvent : UnityEvent<ParamTypeClass> { }

public class myMonoClass : MonoBehaviour
{
    public myInheritedEvent onActionCalled;

    public void myFunction ()
   {
        onActionCalled.invoke (ParamTypeClass);
   }
}

Added this just in case somebody stumbles upon this thread.

6 Likes