- Home /
call one button at time
I have 03 buttons and the player must figure out the sequence. After making his attempt, the script checks the order and restart the game or to move to next stage. How can I do to prevent that the same button be pressed more than once.
Answer by aldonaletto · Jun 03, 2011 at 01:39 PM
Create a boolean var - say, pressed - in the script assigned to the button to indicate if it's pressed. When the character is touching or clicking (or doing whatever it has to do to press the button) the button, check the variable pressed. If it's true, do nothing or play some sound to indicate it's already pressed. If pressed is false, assign true to it and do something to indicate the button is pressed - reduce the length of the button, or move it like it was pressed, or change its color etc:
var pressed:boolean = false;
// in the "button pressing" function check the var pressed:
if (pressed){
// do something to indicate it's already pressed
audio.PlayOnShot(alreadyPressedSound); .
} else {
pressed = true;
audio.PlayOneShot(clickButtonSound); // play a click sound here, if any
// do some modification in the button color or position
// to indicate it was pressed
.
}
Answer by RenatoB · Jun 03, 2011 at 04:09 PM
Thanks adonaletto, but in this case, I can press the button again, right? I need to know how to prevent that´s happens. How to stop the player to press the same button again?
I create this script but not work:
var bt1 = false;
var bt2 = false;
var bt3 = false;
if (hit.gameObject.tag == "button") {
if (bt1 == true)
return;
else
number ++;
bt1 = true;
var lobjetct : GameObject = GameObject.FindWithTag("machine");
lobject.animation.Play ("anim1");
Tried this too:
var bt1 = false;
var bt2 = false;
var bt3 = false;
if (hit.gameObject.tag == "button" && bt1 == false) {
number ++;
bt1 = true;
var lobjetct : GameObject = GameObject.FindWithTag("machine");
lobject.animation.Play ("anim1");