Duplicate Question
How to make a function wait (**I have looked elseware and havent found a well explained solution**)
Title says it all im trying to make my function wait before completing the first action because the function gets called everytime and button is pressed and i want to have a cooldown almost on the time before clicking.
public UnityEngine.UI.Text blocktemperarytext;
private int selected;
private bool waited = false;
private bool clicked = false;
private int[] values = {0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6, 7,7,7, 8,8,8, 9,9,9, 10,10, 11,11, 12, 13, 14};
public void ChooseBlock()
{
//some sort of method to wait here because this function is called every time a button is pressed
selected = values [Random.Range (0, values.Length)];
if (selected == 0) {
PlayerPrefs.SetInt ("Stone", PlayerPrefs.GetInt ("Stone") + 1);
blocktemperarytext.text = ("STONE");
}
if (selected == 1) {
PlayerPrefs.SetInt ("Coal", PlayerPrefs.GetInt ("Coal") + 1);
blocktemperarytext.text = ("COAL");
}
if (selected == 2) {
PlayerPrefs.SetInt ("Iron", PlayerPrefs.GetInt ("Iron") + 1);
blocktemperarytext.text = ("IRON");
}
//these ^^ keep going to 14 but I didnt want to paste them all in because its ridiculously long
$$anonymous$$aybe this function needs to be a coroutine with a WaitForSeconds() at the start of the function. See this answer http://answers.unity3d.com/questions/350721/c-yield-waitforseconds.html
Can a coroutine be called from a button click though?
Call another function from the button that will call the coroutine i.e. void ButtonCick() { StartCoroutine(DelayFunction());}
Follow this Question
Related Questions
I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers
How to wait a certain amount of seconds in C# 4 Answers
Wait time after coroutine's wait seconds is complete 0 Answers
Trying to delay code, nothing works. 2 Answers
WaitForSeconds don't work with variables from another scripts 2 Answers