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”:
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!
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).
I dragged my C# script(ThemeSelectorButton.cs) to ThemeSelectorButton(in Hierarchy).
Now, this C# script appears as an added component in Inspector of this ThemeSelectorButton.
[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.
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.