- Home /
StartCorutine not Working
void OnGUI()
{
if(GUI.Button(new Rect(20,20,120,120), "New Level"))
{
StartCoroutine(Waittt());
if(!LevelGo)
boardObject.CreateGemsGrid();
if(LevelGo){
//boardObject.DeleteGemsGrid();
//boardObject.CreateGemsGrid();
}
LevelGo = true;
menuSriptObj.LevelList();
autoObject.DeleteItems();
autoObject.CreateItems();
// % will make sure to go from beginning when newlevel == numbersOfLevels - 1
newLevel = (newLevel+1) % numbersOfLevels;
print (newLevel);
}
}
public IEnumerator Waittt()
{
yield return new WaitForSeconds(10);
}
This is my script.
Usually after I press new Level I have to wait for 10 seconds and the the bellow code will be runned, right or not?
Answer by FrosTech · Jul 21, 2014 at 12:23 PM
This is a typical error any beginner faces, as I did too.
OnGUI runs every frame, that is it runs atleast (1 to counting) times or more each frame.
So, OnGUI just runs every frame.
This is a simple explanation on OnGUI(). Not related to your question, but its better to know, right??
Coming to your rescue!!
The Waitt() method will run on the next frame, not the current one!
You can verify that by putting a Debug.Log("Print it") within Waittt() method.
It'll print right after 10 seconds just as you want!!
void OnGUI()
{
if(GUI.Button(new Rect(20,20,120,120), "New Level"))
{
StartCoroutine(Waittt());
}
}
public IEnumerator Waittt()
{
yield return new WaitForSeconds(10);
newLevel(); // Includes the task you want to do after 10 seconds have passed.
}
public void newLevel()
{
if(!LevelGo)
boardObject.CreateGemsGrid();
if(LevelGo){
//boardObject.DeleteGemsGrid();
//boardObject.CreateGemsGrid();
}
LevelGo = true;
menuSriptObj.LevelList();
autoObject.DeleteItems();
autoObject.CreateItems();
// % will make sure to go from beginning when newlevel == numbersOfLevels -1
newLevel = (newLevel+1) % numbersOfLevels;
print (newLevel);
}
This should solve your issue for now.
Answer by AyAMrau · Jul 21, 2014 at 12:17 PM
No, the coroutine will wait for 10 seconds and then exit cause it's empty except for the yield statement. If you had any code under it would only be run after 10 seconds.
However a coroutine is a separate thread and while it will wait, the code in the calling function will continue to run.
Therefore any code you want to delay running should be moved to the coroutine after the yield statement. But, beware that unless you disable / destroy the button once it has been clicked once a user will be able to continue doing that and start new coroutine every time (which is probably not what you want).
Answer by Supershandy · Jul 21, 2014 at 12:20 PM
If you're looking to create a new level every ten seconds, you would be better running an InvokeRepeating routine rather than a StartCoroutine like this.
if(GUI.Button(new Rect(20,20,120,120), "New Level"))
{
InvokeRepeating(Waittt(),0, 10f);
And then move everything under the StartCoroutine to Waaaaait() like this.
void OnGUI()
{
if(GUI.Button(new Rect(20,20,120,120), "New Level"))
{
InvokeRepeating(Waittt(), 0, 10f);
}
}
Public void Waittt ()
{
if(!LevelGo)
boardObject.CreateGemsGrid();
if(LevelGo){
//boardObject.DeleteGemsGrid();
//boardObject.CreateGemsGrid();
}
LevelGo = true;
menuSriptObj.LevelList();
autoObject.DeleteItems();
autoObject.CreateItems();
// % will make sure to go from beginning when newlevel == numbersOfLevels - 1
newLevel = (newLevel+1) % numbersOfLevels;
print (newLevel);
}
}
You'll have to forgive some of the coding as I'm at work, but that would be the general jist of what you're trying to achieve, Unity Script Reference qould give you better details on uaing InvokeRepeating()
I should point out that InvokeRepeating in this case would start as soon as the GUI button is pressed (0) and repeat every 10 seconds (10f)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Programming Question with WaitForSeconds 2 Answers
Fire rate for gun script, c# (not u/s) 1 Answer
WaitForSeconds does not work 0 Answers