0

I'm new to events and delegates and I'm having trouble making the functional connection between things, specifically the events manager and how events get connected to specific buttons in my program. So far I've created a few delegates and then I have a class that should subscribe to those delegates but I don't get how this system actually gets connected to buttons on different scenes. I just need someone to help me make the connection so that I can see how everything functions. Thank you.

Here is my event manager class

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

public class Event_Manager : MonoBehaviour
{
    public static Event_Manager evt = null; // create singleton

    public delegate void GoToStartingSceneDelegate();
    public static GoToStartingSceneDelegate onGoToStartingSceneDelegate;

    public delegate void GoToSelectionSceneDelegate();
    public static GoToSelectionSceneDelegate onGoToSelectionSceneDelegate;

    public delegate void GoToColoringSceneDelegate();
    public static GoToColoringSceneDelegate onGoToColoringSceneDelegate;

    public delegate void GoToCaveSceneDelegate();
    public static GoToCaveSceneDelegate onGoToCaveSceneDelegate;

    private void Awake()
    {
        if (evt == null)
            evt = this;
        else if (evt != null)
            Destroy(gameObject);

        DontDestroyOnLoad(gameObject);
    }

    public static void OnStartGameButtonClick()
    {
        Debug.Log("Start Game");
        if (onGoToStartingSceneDelegate != null)
            onGoToStartingSceneDelegate();
    }

    public static void OnStartOverButtonClick()
    {
        Debug.Log("Start Over");
        if (onGoToSelectionSceneDelegate != null)
            onGoToSelectionSceneDelegate();
    }

    public static void OnSendAnimalButtonClick()
    {
        Debug.Log("Send Animal");
        if (onGoToCaveSceneDelegate != null)
            onGoToCaveSceneDelegate();
    }

    public static void OnPlayAgainButtonClick()
    {
        Debug.Log("Play Again");
        if (onGoToStartingSceneDelegate != null)
            onGoToStartingSceneDelegate();
    }
}

and this is the class that I'm wanting to subscribe to those events

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI.ProceduralImage;

    public class LoadSceneButtonController : MonoBehaviour
    {
        [SerializeField] public Object startingScene;
        [SerializeField] public Object selectionScene;
        [SerializeField] public Object coloringScene;
        [SerializeField] public Object caveScene;

        GameObject transitionManager;

        public GameObject touchToPlayButton;
        public GameObject sendAnimalToForestButton;
        public GameObject startOverButton;

    private void OnEnable()
    {
        Event_Manager.onGoToStartingSceneDelegate += GoToStartingScene;
        Event_Manager.onGoToSelectionSceneDelegate += GoToSelectionScene;
        Event_Manager.onGoToColoringSceneDelegate += GoToColoringScene;
        Event_Manager.onGoToCaveSceneDelegate += GoToCaveScene;
    }

    private void OnDisable()
    {
        Event_Manager.onGoToStartingSceneDelegate -= GoToStartingScene;
        Event_Manager.onGoToSelectionSceneDelegate -= GoToSelectionScene;
        Event_Manager.onGoToColoringSceneDelegate -= GoToColoringScene;
        Event_Manager.onGoToCaveSceneDelegate -= GoToCaveScene;
    }

    void GoToStartingScene()
    {
        SceneManager.LoadScene(startingScene.name.ToString());
    }

    void GoToSelectionScene()
    {
        SceneManager.LoadScene(selectionScene.name.ToString());
    }

    void GoToColoringScene()
    {
        SceneManager.LoadScene(coloringScene.name.ToString());
    }

    void GoToCaveScene()
    {
        SceneManager.LoadScene(caveScene.name.ToString());
    }
greyBow
  • 1,298
  • 3
  • 28
  • 62

1 Answers1

1

Your code is not working but the problem has nothing to do with your events or delegates.

The problem is how you declared your scene variables. Do not use UnityEngine.Object for that. If you do, you can only cast it to SceneAsset which is from the UnityEditor namespace. This means that your code will only work in the Editor with the AssetDatabase class.

Use UnityEngine.SceneManagement instead of UnityEngine.Object. You are currently passing Object.name to the SceneManager.LoadScene function and Object.name refers to the name of the Object not a scene name.

Replace all:

[SerializeField] public Object startingScene;
[SerializeField] public Object selectionScene;
[SerializeField] public Object coloringScene;
[SerializeField] public Object caveScene;

with

public Scene startingScene;
public Scene selectionScene;
public Scene coloringScene;
public Scene caveScene;

Here is a simple Unity tutorial for events and delegates. I don't think you need that since that part of your code looks fine.


As for connecting it to your buttons, it is very easy. Just subscribe to the onClick.AddListener button event and notify the Event_Manager event when there is a click.

public Button startingButton;
public Button selectionButton;
public Button coloringButton;
public Button CaveButton;

void OnEnable()
{
    //Register Button Events
    startingButton.onClick.AddListener(() => buttonCallBack(startingButton));
    selectionButton.onClick.AddListener(() => buttonCallBack(selectionButton));
    coloringButton.onClick.AddListener(() => buttonCallBack(coloringButton));
    CaveButton.onClick.AddListener(() => buttonCallBack(CaveButton));
}

private void buttonCallBack(Button buttonPressed)
{
    if (buttonPressed == startingButton)
    {
        Debug.Log("Clicked: " + startingButton.name);
        Event_Manager.OnStartGameButtonClick();
    }

    if (buttonPressed == selectionButton)
    {
        Debug.Log("Clicked: " + selectionButton.name);
        Event_Manager.OnStartOverButtonClick();
    }

    if (buttonPressed == coloringButton)
    {
        Debug.Log("Clicked: " + coloringButton.name);
        Event_Manager.OnSendAnimalButtonClick();
    }

    if (buttonPressed == CaveButton)
    {
        Debug.Log("Clicked: " + CaveButton.name);
        Event_Manager.OnSendAnimalButtonClick();
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328