Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Dok101 · Jan 09, 2014 at 08:39 PM · coroutineyieldupdate functionyield waitforsecondsscripterror

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dok101 · Jan 09, 2014 at 08:49 PM 0
Share

Thank you I believe that that will work

avatar image Griffo · Jan 09, 2014 at 08:53 PM 0
Share

Like SinisterRainbow said you cannot yield in Update only in Coroutines.

Take a look Here loads of great info ..

avatar image Dok101 · Jan 09, 2014 at 08:55 PM 0
Share

I already knew that you couldnt use yield in Update. Thank you though for the link I will check that out

avatar image Dok101 · Jan 09, 2014 at 09:03 PM 0
Share

@Griffo Sorry but for some weird reason I still get Update() cannot be a corountine

avatar image Griffo · Jan 09, 2014 at 09:12 PM 0
Share

Sorry missed line 50 out }

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help with While Loops 1 Answer

Custom Yield Instruction for FixedUpdate frames 3 Answers

Coroutoutine doesnt work properly. 3 Answers

Coroutines Didn't Work After Changing Scene 1 Answer

WaitForSeconds is breaking out of my IEnumerator 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges