Search code examples
unity3d-guiunity-game-engine

Unity UI - Button OnClick() not seeing LevelManager's functions


I'm following the Number Wizard UI lectures in a course on Udemy (I don't know if I can tell the name, but it's the most famous one) and I created the "Start" and "Quit" buttons.

The problem I've encountered is this: in the OnClick() panel on the lower right, the "LoadLevel" function I created (contained in the LevelManager class) doesn't appear.

Consequently, it doesn't work. I tried to look online for it but I can only find stuff regarding the Legacy UI.

Here's a screenshot of what I have in Unity:

Unity Screenshot

And the code of the class:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

    public void LoadLevel(string name) {
        Debug.Log("Level load requested for " + name);
    }

    public void QuitRequest() {
        Debug.Log ("Quit requested.");
    }
}

I hope you can help me, i'll get through the basic Unity tutorials again to check if there's any hint about it.

Thank you for your time!

P.s.: if something's wrong with my question, please keep in mind that this is my first one posted here. Thank you again :)


Solution

  • The problem is very simple: You dragged the script directly from the assets folder into that slot. What you need to do though is to drag the script onto an object in the scene (would be you LevelManager in this case) and then drag that object into the slot.