check if-statement/bool when button is pressed?
Hi i have 4 buttons that all use the same function but with different gameobjects:
public void BuildShip(GameObject ship)
{
Instantiate(ship, mousePos, Quaternion.identity);
}
I know how buttons work and how to call a function with them, however i want to know how to check if that certain button has been pressed. How do i see when OnClick() has been called when clicking the button? How can i use it in a if statement?
What i wanna do is: See if a certain button has been pressed, lets say button2. When button2 has been pressed i wanna for example change a int or call a debug.log etc. Im pretty sure i want to use an if-statement which is called everytime i press the button or i can have a loop to check all the time when a certain button is pressed.
But for now to simply answer this question: I want to know how to see if a button is pressed and how i can check this with the help of an if-statement.
Thanks.
Answer by Ali-hatem · Apr 22, 2016 at 09:23 AM
with script attached to the Buttons :
public void Example(){
switch (gameObject.name) {
case "Button_1":
print (gameObject.name);
break;
case "Button_2":
print (gameObject.name);
break;
}
}
Hey thanks! worked like a charm! To make this work i put this script to the buttons then i assign a new OnClick to the buttons with this script and function. It will now know when i click and then check which button it was and print it!
yes now if the button was button1 do button1 stuffs etc.... & you welcome .