Need Help with Event Handlers

So I am new to Unity and don’t really understand event handlers… I have borrowed some code from a website, but I am getting this error and I don’t know why… I have created an EventHandler and EventManager script…

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

public class EventManager : MonoBehaviour
{
    public delegate void OnRest();              //Declare a Delegate
    public static event OnRest onReset;         //Create an Event
    public delegate void OnReStart();           //Declare a Delegate
    public static event OnReStart onReStart;    //Create an Event
    public static void RaiseOnReset()
    {
        if (onReset != null)
        {
            onReset();                          //Invoke an Event
        }
    }
    public static void RaiseOnReStart()
    {
        if (onReStart != null)
        {
            onReStart();                       //Invoke an Event
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventHandler : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            EventManager.RaiseOnReset();                   // Function call to invoke an Event
        }
        else if (Input.GetKeyDown(KeyCode.K))
        {
            EventManager.RaiseOnReStart();                // Function call to invoke an Event
        }
    }
}

and I also added code to my script that I want to activate the event on here…

public GameObject EventManager;

 
    void Start()
    {

        EventManager.onReset += Reset;
        EventManager.onReStart += Restart;

    }

    public void Reset()
    {

    }
    public void Restart()
    {

    }

and with the public gameobject I also dragged in my gamemanager containing my code for both the event handler and manager.

however it throws me an error saying “error CS1061: ‘GameObject’ does not contain a definition for ‘onReset’ and no accessible extension method ‘onReset’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)”

If someone could explain this for me it would be very helpful. :slight_smile:

So i have barely used Event Systems but i always come back to this video that has a great explanation and demo of them in use.

I know this isnt the answer to your problem but it should help you understand Event Systems better. It is also pretty short so it won’t take you long to rework it if you have to.

1 Like

Honestly I reworked my code and it helped explain a lot more than I knew at first, so thank you, but I still have one question now…
Everything works fine with no errors, however he uses a different event trigger than what I want to do. At 4:13 he uses a OnTriggerEnter(Collider other) but I don’t want to use a trigger, I want to use a key press… this is what I have so far, but it doesn’t do anything, so the event is not triggered…

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

public class EventHandler : MonoBehaviour
{
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Y))
        {
            EventManager.current.MirageStarted();
        }
    }
}

So how do I add code to where if I press the key Y, then the event is triggered?

Update:Just got it! It wasn’t a problem with this code above at all, instead my start function didn’t work on the code that actually used the event, so I transfered the subscription to an awake function and it worked perfectly.

Glad you got it figured out! That video helps me out all the time since i only use Event Systems sparingly. I need to use them more.