block button click

Hello there,

I have a button and when its clicked some animation happens. What I want to do is after clicking the button, user should not be able to click the same button for upcoming 2 seconds. Just like blocking button click for next 2 seconds and then again activating the button for animation.

May I know how would I do this?
something like this

var dontLetUserClickButtonOneForTwoSeconds : boolean = false;
var buttonOneXOrigin : int = 110;
if(dontLetUserClickButtonOneForTwoSeconds)
{
	
		buttonOneXOrigin = -100;
		yield WaitForSeconds(25);
		buttonOneXOrigin = 110;
}

if(GUI.Button(buttonOneXOrigin,95,75,75))
{
dontLetUserClickButtonOneForTwoSeconds = true;
//some animation code
}

I tried this, but it does not work.

Thanks

Maybe something like this:

var pauseClickTime : int = 2;
private var buttonOneXOrigin : int = 110;
private var initPauseClickTime : int = 0;

function Start() {
	initPauseClickTime = pauseClickTime;
}

function AnimateButton() {
  buttonOneXOrigin = 200;
  yield WaitForSeconds(2);
  buttonOneXOrigin = 110;	
  pauseClickTime = initPauseClickTime;
}

function OnGUI() {
	if (GUI.Button(Rect(buttonOneXOrigin, 95, 75, 75),"Click Here")  pauseClickTime != 0) {
		pauseClickTime = 0;
		AnimateButton();
	}
}

Also, setting your button to a negative will move it off screen. Hope this helps you out.

-Raiden

Hello Raiden,

That was an interesting approach. I think that will work. and yes I forgot to mention one thing. I have texture over the button. So user will have this feeling of button being there but its inactivated for some interval of time.

I will try out and post the results.

Thank you very much!

Thanks Raiden, it worked nicely :slight_smile:

GUI.enabled = false will make a nicely dimmed out and unclickable button.

Your code could look something like

var CLICKABLE_DELAY : float = 2.0;
private var isButtonClickable : boolean = true;

function OnGUI() {
  GUI.enabled = isButtonClickable;
  if(GUI.Button())
  ButtonClicked();
  GUI.enabled = true;//otherwise all the rest of your GUI would be disabled too.
}

private function ButtonClicked() {
  isButtonClickable = false;
  //various code here
  yield WaitForSeconds(CLICKABLE_DELAY);
  isButtonClickable = true;
}