I can not figure out how to use delegates and events in a Unity project.
I have an observer class
public class Observer : MonoBehaviour { #region Singletone private static Observer _instance; public static Observer Instance { get { if (!_instance) { _instance = FindObjectOfType(typeof(Observer)) as Observer; if (_instance == null) { Debug.Log("Error!"); } } return _instance; } } #endregion public delegate void GivePoints(int value); public static event GivePoints OnGivePoints; } I have a Death method in a zombie script:
public void Death() { if (Observer.OnGivePoints != null) { Observer.Instance.OnGivePoints(5); } } public void GivePoints(int value) { Debug.Log(value); } Also in the start method in the zombie script it is written:
void Start() { Observer.OnGivePoints += GivePoints; } It seems to have done everything correctly, but in the Observer.OnGivePoints != null swears at OnGivePoints , writes: " Observer ')
I used the Observer pattern for this video tutorial, and I also took the code from there - https://www.youtube.com/watch?v=qwQ16sS8FSs , for some reason everything works for him