I have a C# script called MainMenu.cs

Here it is in its entirety

using UnityEngine;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour
    public Button btnPlay;

    // Use this for initialization
    void Start ()
        btnPlay = btnPlay.GetComponent<Button>();

    public void PlayLevel()

In my hierarchy, i have a Main Camera, a Canvas and EventSystem. Under Canvas i have a Button. I add the MainMenu script to the Canvas and associate the button with Button i have in canvas. When i go to the button and associate the MainMenu with its OnClick I am expecting to be able to chose PlayLevel() but it is not there.

Am i missing something obvious here? (Using Unity 5.0)

enter image description here

  • \$\begingroup\$ btnPlay = btnPlay.GetComponent<Button>(); Uh. I don't think you need this. Either btnPlay is already assigned (in which case this code is a NOP) or it isn't (in which case this code will Null Pointer). \$\endgroup\$ – Draco18s Sep 29 '18 at 2:01

Because the copy of "MainMenu" you selected is not the MainMenu component in your scene. Its the one in your assets folder. Be sure to find the one in your scene. Then you will see the method. enter image description here

  • \$\begingroup\$ Looks like i am doing something really silly here... What would be the reason for the script not showing in my scene? I had a default scene and when i made a new one i started getting this issue. The MainMenu does not show up in the Object selection pane that you have in your screenshot. How do i make it show up in there? I thought all scenes can use these scripts (as long as you attach the right script) \$\endgroup\$ – Dust May 16 '15 at 22:26
  • \$\begingroup\$ You need to have that script attached to a game object that is in the scene. Add the main menu as a component to a gameobject in your scene. Maybe the canvas for the button is a good choice. Then that MainMenu should be selectable by choosing the right object and script in the SCENE tab of the object selection screen. \$\endgroup\$ – Plastic Sturgeon May 17 '15 at 20:09
  • \$\begingroup\$ yes, thats exactly what i've done. I added "MainMenu" to the canvas in the scene. That's what is so weird about this. A button is child of that canvas and i add the same script to the button and it has a public method but i don't see it for some reason. \$\endgroup\$ – Dust May 17 '15 at 22:06
  • 1
    \$\begingroup\$ Good. - You have the canvas and the canvas has the main menu script. In the OnCLick, select the canvas Object from the scene. Then select the Function Dropdown - you should see the "MainMenu" there and be able to select PlayLevel in a submenu for "MainMenu". \$\endgroup\$ – Plastic Sturgeon May 18 '15 at 19:14
  • 1
    \$\begingroup\$ Here's how I think of it: You onClick could trigger any number of MainMenu.PlayLevel methods attached to any number of objects in the scene. So you have to select the object first, and then the method so that it calls the CORRECT PlayLevel. \$\endgroup\$ – Plastic Sturgeon May 19 '15 at 17:33

Newbie, just figured out how to do it:

  1. create an empty gameobject under your scene.
  2. drag the C# script to the empty gameobject's inspector, rename the gameobject to something easy to understand, e.g. "MyScript".
  3. under button onclick event, click + to create a new entry.
  4. in the new entry's gameobject field, drag the "MyScript" object to this field.
  5. open the functions menu, your functions will show up.

Other things to consider: 1. make sure your script does not have any build errors. 2. make sure your script's functions are public. 3. make sure your function's has only 0 or 1 basic type parameter.

This will do the trick. Unity UI system is really weird BTW. Good Luck!


To build off of BuildThatGame's answer, I had the Game Object and I had the script already in my current scene, but the functions weren't showing up.

The reason was because I was dragging the script onto the button's OnClick event, not the game object I made. If you follow BuildThatGame's answer and still can't figure out why the functions aren't showing up, check that you added the game object.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.