system
April 19, 2011, 9:00pm
1
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!
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());