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.