- Home /
Update() can not be a coroutine.
Hello I keep having this problem "Update() can not be a coroutine." popping up in my console. What I am attempting to make is a menu. What the script that is showing the error does is handle changing the selected button in the menu and changing scene depending on what button is checked.
Here is my code var Menu : int = 1; function Start () { } function Update () { if (Input.GetKeyDown (KeyCode.UpArrow)) { Menu = Menu + 1; yield WaitForSeconds (1); } if (Menu > 5) { Menu = 1; } if (Input.GetKeyDown (KeyCode.UpArrow)) { Menu = Menu - 1; yield WaitForSeconds (1); } if (Menu < 1) { Menu = 5; } if(Input.GetKeyDown(KeyCode.Return) && (Menu == 1)); { Application.LoadLevel("Multiplayer"); } if(Input.GetKeyDown(KeyCode.Return) && (Menu == 2)); { Application.LoadLevel("Career"); } if(Input.GetKeyDown(KeyCode.Return) && (Menu == 3)); { Application.LoadLevel("Challenges"); } if(Input.GetKeyDown(KeyCode.Return) && (Menu == 4)); { Application.LoadLevel("Free Race"); } if(Input.GetKeyDown(KeyCode.Return) && (Menu == 5)); { Application.LoadLevel("My Garage"); } }
As you can see its a pretty plain and simple script just handling changing the scene. My problem is that I get the error "Update() can not be a coroutine." From my research its because the Update Function cannot handle yield. However I have tried using the CoUpdate function but that does not work. Because of this one of my questions is what function is recommended to use in place of Update. If I take out the yield for some reason it goes to the "My Garage" scene even though I dont press any button and the Menu variable = 1.
Although I am sure that the answer is simple I cant seem to figure it out.
Thanks In advance
Answer by Griffo · Jan 09, 2014 at 08:48 PM
Try this
function Update()
{
if (Input.GetKeyDown (KeyCode.UpArrow))
{
Menu = Menu + 1;
wait();
}
if (Menu > 5)
{
Menu = 1;
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
Menu = Menu - 1;
wait();
}
if (Menu < 1)
{
Menu = 5;
}
if(Input.GetKeyDown(KeyCode.Return) && (Menu == 1));
{
Application.LoadLevel("Multiplayer");
}
if(Input.GetKeyDown(KeyCode.Return) && (Menu == 2));
{
Application.LoadLevel("Career");
}
if(Input.GetKeyDown(KeyCode.Return) && (Menu == 3));
{
Application.LoadLevel("Challenges");
}
if(Input.GetKeyDown(KeyCode.Return) && (Menu == 4));
{
Application.LoadLevel("Free Race");
}
if(Input.GetKeyDown(KeyCode.Return) && (Menu == 5));
{
Application.LoadLevel("My Garage");
}
}
function wait(){
yield WaitForSeconds (1);
}
Like SinisterRainbow said you cannot yield in Update only in Coroutines.
Take a look Here loads of great info ..
I already knew that you couldnt use yield in Update. Thank you though for the link I will check that out
@Griffo Sorry but for some weird reason I still get Update() cannot be a corountine
Answer by SinisterRainbow · Jan 09, 2014 at 08:47 PM
yield WaitForSeconds (1); <-- yields can only be used in Coroutines. Not sure why you want to delay here anyway, but hey. just remove those yields and no more coroutine errors.
Edit: I should mention they can also be used in OnCollisionEnter and some like functions.