Function Not Appearing In OnClick Editor
Asked Answered
J

14

5

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!

Juneberry answered 31/1 at 15:37 Comment(0)
A
0

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).

Arms answered 26/3 at 4:4 Comment(3)

I imagine it should also be recommended that this "attached GameObject" be an EventsSystem. I take it that's what it's there for? Thanks!

Juneberry

Also the method needs to have zero or one parameter, or it will not show up.

Garibald

You can indeed attach it to the button itself and it works fine.

Seato
R
0

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

Riedel answered 6/6, 2023 at 2:24 Comment(0)
L
0

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.

Legend answered 9/3 at 23:33 Comment(1)

I solved my issue of the fonction not showing by adding a Static instance of my MenuManager script which allows it to be accessed by any other script and the OnClick Editor. Just adding theses two lines: public static MenuManager Instance; void Awake(){ Instance = this; }

Tbilisi
D
0

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.

Dihedral answered 28/1 at 14:34 Comment(3)

OMG that was a silly one but I guess pretty obvious once you know it... Thanks dude

Slider

It also helps to ensure you have all required semi colons and no compile errors. I missed a compile error in the bottom left which is easy to miss since the error message is so small on a big monitor.

Dieterich

Yeah, I thought it was only private if you wrote private before void. I guess not! thx for the idea.

Taciturnity
C
0

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!

Cowrie answered 6/2 at 20:51 Comment(4)

Your answer actually solved my problem. I was in Assets every time. Thanks a lot! :)

Pavior

This answer helps. [165448-註解-2020-08-14-003101.png|165448]

Metameric

thank you this worked!!!

Indurate

This was the issue for me -- thank you!!!

Knight
D
0

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

Dielle answered 29/7, 2019 at 22:55 Comment(0)
H
0

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.

Hyunhz answered 7/12, 2023 at 12:13 Comment(0)
M
0

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.

Madrigal answered 21/10, 2019 at 22:35 Comment(0)
P
0

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

Prepotent answered 10/1, 2020 at 21:47 Comment(0)
S
0

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

Staggs answered 29/1 at 19:34 Comment(1)

Thank you so much! This was my problem. My function was defined to return bool instead of defined as void return.

Norm
S
0

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.

Spellman answered 3/2, 2022 at 4:49 Comment(0)
C
1

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...
}
Cognoscenti answered 31/1 at 15:40 Comment(0)
G
0

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!

Gelsenkirchen answered 17/10, 2023 at 8:44 Comment(1)

This was my issue, thanks! I had an enum as the parameter and it wouldn't show up until I changed it to a simple type.

Alf
E
0

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.

Estimative answered 31/1 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.