- Home /
Cant Make C# Use waitforseconds Command
I'm trying to make a little game based off the Roll-A-Ball tutorial game, and I want to make it so that once you've collected all the cubes, the game waits 4 seconds and then starts the next scene/level. However, the waitforseconds command just refuses to work. This is the code I'm using:
void SetCountText ()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winText.text = "YOU WIN!";
winSound.GetComponent<AudioSource>().Play ();
yield return new WaitForSeconds(4);
SceneManager.LoadScene("Level2");
}
}
When I run the debug it says that 'void is not an iterator block', which I don't know what it means. The Scripting API appears to say that the 'IEnumerator' command has to be in place of the void command, but when I do that it doesn't work at all either. Some other questions I've found say that you have to use a 'Coroutine' command, which the Scripting API doesn't say anything useful about other than having the 'IEnumerator' in the example code. Why can't I just use the WaitForSeconds command normally?
Answer by gjf · Mar 27, 2016 at 09:36 PM
IEnumerator
is not a command, it's a type - if you want to use WaitForSeconds()
then you have to call it as a coroutine. change the function to:
IEnumerator SetCountText()
and call it with
StartCoroutine(SetCountText());
I tried it, but it didn't work.
I changed it to this:
IEnumerator SetCountText()
{
StartCoroutine(SetCountText());
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winText.text = "YOU WIN!";
winSound.GetComponent<AudioSource>().Play ();
yield return new WaitForSeconds(4);
Scene$$anonymous$$anager.LoadScene("Level2");
}
}
It also broke that 'section' (For lack of a better word.) of the whole script, meaning that it didn't display how many cubes the player collected and didn't show the 'you win' text after 12 of them are collected. Am I doing it wrong?
Also, is there some explanation of C# syntax somewhere? Like what exactly a 'type' is and what 'calling' means?
You don't want to start the coroutine from within the coroutine. If SetCountText is called from elsewhere that means every time it starts it is going to try and start another instance of itself and go into an endless loop. If it isn't called from elsewhere first then it is just never going to start.
Remove StartCoroutine(SetCountText());
and use that outside of the IEnumerator wherever it is that you want to start the coroutine from ie:
void Start()
{
StartCoroutine(SetCountText());
}
IEnumerator SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winText.text = "YOU WIN!";
winSound.GetComponent<AudioSource>().Play ();
yield return new WaitForSeconds(4);
Scene$$anonymous$$anager.LoadScene("Level2");
}
}
Answer by sunderplugs11 · Mar 28, 2016 at 05:13 AM
You really need to watch this
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
Answer by Bleackk · Mar 28, 2016 at 03:01 PM
IEnumerator changeDelay ()
{
SetCountText ();
yield return new WaitForSeconds (10);
SceneManager.LoadScene ("Level2");
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 1 && !counting)
{
counting = true;
winText.text = "Good Job";
}
}
}
Your whole concept of the code was wrong from the start. What you have to do is start a Corouting of any name in this case it " changeDelay" Get a Void SetCountText put in there the whole code with the score and win text then in the IEnumerator you need to put in the whole Function, THEN wait for 10 seconds and change the whole level. If this does not work then something else in your code is wrong you might want to put in the whole thing.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Pause a script completely 3 Answers
Wait in seconds before damaging the player again 2 Answers
Problem getting AI to wait 1 Answer
Distribute terrain in zones 3 Answers