- Home /
My function doesn't appears in the OnClick button script
using UnityEngine; using System.Collections;
public class Database : MonoBehaviour {
public int numSaves;
public int[] save = new int[4];
public string[] nome = new string[4];
public bool[] pinOn = new bool[4];
public int[] pin = new int[4];
public void Salvar (string nome_, bool pinOn_, int pin_) {
if (numSaves < 5)
{
save[numSaves + 1] = save[numSaves + 1];
nome[numSaves + 1] = nome_;
pinOn[numSaves + 1] = pinOn_;
if(pinOn[numSaves + 1] != false)
{
pin[numSaves + 1] = pin_;
}
}
}
}
Ah, didn't see that image link right away, I see what you mean now. Do you get any compiler errors on your Database Script?
I have the same issue, did you find a solution for this? I am using UnityEngine.UI
![using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UpdateValue : $$anonymous$$onoBehaviour {
public Text text;
// public Slider slider;
// Use this for initialization
// Update is called once per frame
public void UpdateText(float f) {
Debug.Log ("f" + f);
text.text = f.ToString();
}
}][1]
Hi Guys,
I have the same issue, however I am using UnityEngine.UI; And also only 1 float. I noticed others have issues as well...
I tried adding an empty object with script attached to it but onclick() doesn't take objects.
Any idea why I can only select $$anonymous$$onoScript and not actually the function, I am hoping I am doing something wrong! [1]: /storage/temp/45146-screenshot.jpg
Answer by Landern · Apr 24, 2015 at 12:36 PM
You have to many parameters. An OnClick event should be a public, void with one or no parameters/arugments. You have 3 parameters in your method, the single parameters should be either an int, string, bool, UnityEngine.Object or float by type.
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Answer by CorruptedTNC · Apr 24, 2015 at 12:37 PM
You can't call a function OnClick in the editor if it has more than 1 inbound parameter.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public void FunctionWorks(string name)
{
Debug.Log(name);
}
public void FunctionDoesntWork(string first, string second)
{
}
}
Answer by Teejs · Mar 12, 2017 at 07:31 PM
As Landern has pointed out, the OnClick event can only accept a single int, string, bool, UnityEngine.Object or float by type. But there is a workaround solution that can pass in multiple parameters. That is, create a utility class that contains the parameters you would like to pass in, and then pass in that class. For your case, what you can do is:
//a separate utility class
public class SalvarInfo : MonoBehaviour{
public string nome_;
public bool pinOn_;
public int pin_;
}
public int numSaves;
public int[] save = new int[4];
public string[] nome = new string[4];
public bool[] pinOn = new bool[4];
public int[] pin = new int[4];
public void Salvar (SalvarInfo info) {
if (numSaves < 5)
{
save[numSaves + 1] = save[numSaves + 1];
nome[numSaves + 1] = info.nome_;
pinOn[numSaves + 1] = info.pinOn_;
if(pinOn[numSaves + 1] != false)
{
pin[numSaves + 1] = info.pin_;
}
}
}
Answer by ForceVII · Feb 12, 2021 at 11:17 PM
If none of these work for you, probably you have parameter for you void but you arent using them. Use them or try removing parameter. example:
public void Start(int sceneIndex)
{
SceneManager.Load(1);
}
So remove "int sceneIndex" parameter from your void or use it in void.
I dont know why but it worked for me.
$$anonymous$$e:
It doesnt work. Why?
It works. Why?
Your answer
Follow this Question
Related Questions
My Function is not showing up in the OnClick Menu 2017.2.0f 0 Answers
what is wrong whis this? 1 Answer
Integer Getter Method always returns zero 3 Answers
Multiple Cars not working 1 Answer