- Home /
Getting an error when trying to yield
I am trying to make a make shift credits screen that replaces a texture every five seconds to show what a new person was involved in. Example:
if (who == 0)
{
guiTexture.texture = tex1;
yield WaitForSeconds(5);
who = 1;
}
if (who == 1)
{
guiTexture.texture = tex2;
yield WaitForSeconds(5);
}
I get an error saying: Update() can not be a coroutine.
Any suggestions to fix this? Thanks in advance!
Answer by Mortennobel · Apr 19, 2011 at 09:05 PM
Checkout the following answer:
http://answers.unity3d.com/questions/48525/what-function-can-be-a-coroutine
Answer by Bunny83 · Apr 19, 2011 at 09:12 PM
Update is called around 50-70 times a second. You can't suspend it for 5 sec. Just use a seperate coroutine for that. Start() can be a coroutine, use that:
function Start()
{
guiTexture.texture = tex1;
yield WaitForSeconds(5);
guiTexture.texture = tex2;
yield WaitForSeconds(5);
// ...
}
or in C#
IEnumerator Start()
{
guiTexture.texture = tex1;
yield return new WaitForSeconds(5.0f);
guiTexture.texture = tex2;
yield return new WaitForSeconds(5.0f);
// ...
}
If you don't want to start the coroutine at the beginning use a seperate function
function ShowCredits() { guiTexture.texture = tex1; yield WaitForSeconds(5); guiTexture.texture = tex2; yield WaitForSeconds(5); // ... }
// Somewhere trigger ShowCredits ShowCredits();
In C# you have to start a coroutine explicitly with StartCoroutine
StartCoroutine(ShowCredits());
Your answer
Follow this Question
Related Questions
What is wrong with this use of WaitForSeconds? 1 Answer
Alternative to "yield return WaitForSeconds()" in Coroutine with .NET Framework (Unity 2018.4) 0 Answers
Coroutine sequence not running properly 1 Answer
yield works, but yield WaitForSeconds does not? 1 Answer
WaitForSeconds problem with Unity Pro 3 Answers