Function Not Appearing In OnClick Editor

I have just started working with Unity, so I’m experiencing all the thrills and joy of learning a new programming suite from the ground up. All I want to do is create at OnClick function and link it to my button. Here is the code I’ve got:

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

public class btnSceneSelect : MonoBehaviour
{
    public void LoadSceneSelect ()
    {
        SceneManager.LoadScene( 1 );
    }
}

Here is a screenshot of the LoadSceneSelect function NOT appearing for selection for use with the OnClick event. The only option that appears under Monoscript is “string name”:

I think we can all agree that LoadSceneSelect is a public void function with no parameters.

I would appreciate any insight that could be offered. Thank you!

5 Likes

Instead of selecting the C# class, select the GameObject that btnSceneSelect is attached to.

If btnSceneSelect is not attached to a GameObject, attach it to one (other than the button).

When you clicked on On Click() little circle to add your Script, you need to be under the Scene tab, not the Assets tab. Hopefully this helps!

If this is the Happiecat vids, you may also need to change the coding in script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
  
public class SceneSelectButton : MonoBehaviour
{
    public void LoadSceneSelect()
    {
        SceneManager.LoadScene(1);
    }
}

That should be all you need to change to get buttons to work!

Kinda late, but if anyone else is facing this issue, make sure that the function in your script is set to ‘public’ ( ie public void function() ) instead of just void funtion() which is sometimes set to private by default. Should work after that.

Hi guys, I know this is an old post, but for me what happened was that I was requesting a type Enum as the function’s first argument.
It only shows up if I change that to a more predictable type like string, int, etc. I’m guessing Unity doesn’t really know how to handle custom Enums in the UI.
Hope this helps other people with the same issue!

Select the button as a gameobject instead of the script

i mean you have attached the script containing the script to that button

Drag that button into the field instead of the script

Also you can do it via script too

Above solutions works depending upon your scenario.

My scenario: Prefab
I am trying to attach onClick for a button on (ThemeSelectorButton) which is part of my Prefab (ThemeSelectorGroup).

  1. I dragged my C# script(ThemeSelectorButton.cs) to ThemeSelectorButton(in Hierarchy).
  2. Now, this C# script appears as an added component in Inspector of this ThemeSelectorButton.
  3. [Imp] Now, instead of dragging C# script from Project/Assets/Scripts, drag it from the newly added component(Script) formed in Step 2, onto None(Object) under “On Click” of the Button(in Inspector)

3rd step gives you all the public methods in dropdown and not just MonoScript.

I seen this so many times everywhere and doesnt help me.

I am using a game object with script attatched.

I can see the script name and a bunch of other functions but not the methods I put in the script.

They are public void and no parameters.

Im doing everything right cant get it to show up.

it can be work that your function to define void instead of any type

In my case,

The function not showing up on the onClick Editor is because my function is with two parameters.

//Two parameters case, not showing
public void myFunction(string value1, string value2)
{
      //myFunction...
}

Then I change to this, it works, but you have to pass in the value in combination and split it inside the function.

Example:

  1. value1 = "something"
  2. value2="another"
  3. then pass it in one value="something|another"
public void myFunction(string value)
{
     string value1 = value.Split('|')[0];
     string value2 = value.Split('|')[1];
      //myFunction...
}
1 Like

As Ironic as it may seem, I had to restart Unity for my function to appear on the OnClick() menu.

I use Hot Reload and I think that the function wasn’t detected because of it.

make sure that you attached the script to your game object because sometimes it gets removed without any reason

One more thing to note, if the function you want to use modifies any non-public (or otherwise out of scope) properties of the class, then it will be unavailable regardless of the function’s scope.

you need to drag and drop Game Object you attached the button to.
Example :
Button1 inside panel1
you have to drag and drop panel1 in OnClick inside button1 script.
because you attached the script to panel1
then you will find the Public functions only available in last one (which is the name of the script) of the listview

Certain functions won’t show up in the list. If your function has any non-primitive types in its parameter list, the button won’t be able to press it. It can take int, string, bool, float, and so on, including MonoBehaviour and its subtypes. But it doesnt work on any struct or enum type. It also accepts methods with no parameters.