- Home /
More than one Button Onclick instruction causes the script to run as many time
Can somone help me, I have one button in which I added 3 steps for the OnClick event. 2 which disable the button and image and one that call the script test and print the number of time the function is called: Here is the Test code:
public class Test : MonoBehaviour {
int i = 0;
public void Pressed()
{ i++;
Debug.Log("Button was pressed:" + i);
}
}
The function should only be called once but it is called 3 times, exactly the instructions i have in the OnClick event. If I only let the script it runs normaly:
I created a game in Unity 2017.1 and didn't have this problem. Any help will be appreciated.
Sorry, but your information is just confusing and not clear.What does this sentence mean:
If I only let the script it runs normaly:
Also you said at the end:
I created a game in Unity 2017.1 and didn't have this problem.
This is the first time you actually mention a Unity version and you said you don't have this problem, When do you actually have the problem? Have you checked the stacktace for each of the debug.log calls in the console?
You need to be much clearer when you ask questions. We don't know anything about your project and it's not even possible to follow what we have to do to reproduce this problem. What Unity version, What OS ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612140507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 5.0.2f1 UI Button OnClick Function 6 Answers
UI Button OnClick Function - How to get name of button that was clicked? 2 Answers
UI Button generated in runtime OnClick event 0 Answers
How do change UI.button onclick states(off, editor and runtime, runtime only) via script? 0 Answers
Functions aren't appearing in the onClick editor..... 1 Answer