- Home /
how to make a button click able after a few seconds
every rpg game has skills and when u click on them then there is a cooldown time where you cant click on the button again im trying to make that in unity but its not working i dont want to use WaitForSeconds becuase that dosent work if i use that then the button wount show up any help plizz
Here is an answer I wrote for a similar case - but it does exactly what you want. It uses WaitForSeconds in a co-routine and a boolean to toggle the button being displayed or not :
Answer : http://answers.unity3d.com/questions/231521/how-to-display-data-in-an-array.html
Example : http://www.alucardj.net16.net/unityanswers/hintbutton1-1.html
There's abosolutely nothing stopping WaitForSeconds from working in this situation. Just think about it more, it's pretty obvious what you have to do.
Answer by AlucardJay · Mar 28, 2012 at 05:07 AM
Here's a solution using a counter :
var myCounter : float = 0.0;
var mySetTime : float = 2.5;
function OnGUI () {
// when my set time is reached
if (myCounter >= mySetTime) {
// show GUI Button
if (GUI.Button(Rect(10, 10, 100, 35), "Start Again")) {
myCounter = 0.0;
}
}
// add time to counter
// use myCounter++; (same as myCounter+=1;) to count the frames - or
myCounter += Time.deltaTime; //
// display counter
GUI.TextField (Rect((Screen.width/2)-100, 10, 200, 25), "myCounter : " + myCounter);
GUI.TextField (Rect((Screen.width/2)-100, 40, 200, 25), "mySetTime : " + mySetTime + "secs");
}
Can someone please help me ? I used this script and indeed works flawlessly, but I need it to trigger the cooldown by using keys from the keyboard and not mouse click.
Any ideas ?
if(input.Get$$anonymous$$ey($$anonymous$$eyCode.A) {
if (GUI.Button(Rect(10, 10, 100, 35), "Start Again")) {
myCounter = 0.0;
}
}
$$anonymous$$aybe? Im not good at this but you can test :)
Answer by static_cast · Nov 11, 2013 at 03:13 PM
Here is some psuedocode because I don't have much experience with GUIButtons that might work:
var buttonClicked = false;
function OnButtonClick() //DON'T KNOW IF THIS FUNCTION EXISTS
{
if(buttonClicked == false)
{
buttonClicked = true;
//CARRY OUT BUTTON STUFFS HERE
yield WaitForSeconds(1.0); //CHANGE THIS TO THE NUMBER OF SECONDS YOU WANT TO WAIT FOR
buttonClicked = false;
}
}
Your answer
Follow this Question
Related Questions
GUI & GUI Text Disappear When Publishing 6 Answers
Why my button in for loop isn't working? 1 Answer
Deactivating a power up script after a certain amount of time 1 Answer
Usage timer/weapon cooldown... 3 Answers
Very Very Precise Timer ? 0 Answers