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 UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class btnSceneSelect : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
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!
Answer by TBruce · Aug 07, 2016 at 06:52 PM
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).
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!
Also the method needs to have zero or one parameter, or it will not show up.
So if it has two parameters, it will not work? Is there a workaround for that? I have a function that will set the screen resolution, so I need two parameters.
Correct, you should be able to use an array or a custom object to do that. See more here: https://answers.unity.com/questions/785134/ui-button-multiple-parameters.html
You can indeed attach it to the button itself and it works fine.
Answer by tyfoon3s · May 08, 2017 at 06:43 AM
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.
O$$anonymous$$G that was a silly one but I guess pretty obvious once you know it... Thanks dude
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.
I had compile errors that I wasn't paying attention to yet. This was preventing unity from actually updating my list to contain my new functions.
Answer by PutridCarcass · Mar 14, 2018 at 09:41 PM
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
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void LoadSceneSelect()
{
SceneManager.LoadScene(1);
}
}
That should be all you need to change to get buttons to work!
Your answer actually solved my problem. I was in Assets every time. Thanks a lot! :)
Answer by sambid123sameer · Aug 07, 2016 at 07:20 PM
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
http://answers.unity3d.com/questions/779226/unity-46-getcomponentonclick-how-do-you-add-an-eve.html
Answer by BradOZman · Oct 13, 2016 at 04:24 PM
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.
I solved my issue of the fonction not showing by adding a Static instance of my $$anonymous$$enu$$anonymous$$anager script which allows it to be accessed by any other script and the OnClick Editor. Just adding theses two lines:
public static $$anonymous$$enu$$anonymous$$anager Instance;
void Awake(){
Instance = this;
}