Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Xzapo · Jun 09, 2015 at 02:23 PM · c#error messageupdate

Error cannot be an iterator block

So I ran into this error in my update function and I have no idea what it means, or what happened. It seemed to happen when I added my pause to my if statement. Here's the error...

error CS1624: The body of redical.Update()' cannot be an iterator block because void' is not an iterator interface type

Here's my code

     void Update () {
         RaycastHit hit;
         float distance;
         float waitTime;
 
         waitTime = 3.0f;
 
         if (Physics.Raycast (new Ray (CameraFacing.transform.position, CameraFacing.transform.rotation * Vector3.forward), out hit)) {
             distance = hit.distance;
             if(hit.collider.gameObject.tag == "found"){ 
                 yield return new WaitForSeconds (waitTime);
                 if(hit.collider.gameObject.tag == "found"){
                     foundObject = hit.collider.gameObject; 
                     Destroy(foundObject);
                     //x++;
                     //print(x);
                 }
             } 
         } else {
             distance = CameraFacing.farClipPlane * 0.95f;
         }
         transform.position = CameraFacing.transform.position + CameraFacing.transform.rotation * Vector3.forward * distance;
         transform.LookAt (CameraFacing.transform.position);
         transform.Rotate (0.0f, 180.0f, 0.0f); 
     }
 }
Comment
Add comment · Show 2
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 Bonfire-Boy · Jun 09, 2015 at 02:43 PM 0
Share

You can't have a "yield return" in there because Update doesn't (and can't) return an IEnumerator.

Generally speaking you should only be putting "yields" in Coroutines (which must return IEnumerators).

You probably want to take this code out of Update and put it in a coroutine (which you could start from the update function).

avatar image dubbreak · Jun 09, 2015 at 08:02 PM 0
Share

You don't want to be waiting in an update routine anyhow (blocking your updates will hurt your FPS).

If you need to do something else in response (that is going to delay or take some time) you should be spinning off a coroutine so it doesn't block the main thread.

1 Reply

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

Answer by pako · Jun 09, 2015 at 02:42 PM

The "yield return new WaitForSeconds (waitTime)" statement (line 11) is what's causing the error. It should only be used inside iterator blocks (IEnumerator):

http://docs.unity3d.com/ScriptReference/WaitForSeconds.html

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

24 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Need to update Material/Renderer before they can be applied? 0 Answers

After instantianting gameobjects, unity cant asign references anymore 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