Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Pagwin · Nov 29, 2017 at 11:30 PM · errorerror message

I have a problem with my code please help

Update has an error which is "The body of 'movement.Update()' cannot be an iterator block because void is not an iterator type" it repeats that message(minus the double quotes) 4 times any help would be welcome

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movement : MonoBehaviour {
     // Use this for initialization
     void Start () {
         Cursor.lockState = CursorLockMode.Locked;
     }
     
     // Update is called once per frame
     void Update () {
         float Horizontal = Input.GetAxis("Horizontal"), Vertical = Input.GetAxis("Vertical");//this gets the wasd and arrow key input
         transform.Translate(Horizontal * Time.deltaTime * 5, 0f, Vertical * Time.deltaTime * 5);//this moves the player
         float MouseX = Input.GetAxis("Mouse X"), MouseY = Input.GetAxis("Mouse Y");//gets the mouse positino
         transform.Rotate(-MouseY,MouseX,0);//rotates the player as they move the mouse
         Vector3 playerRot = transform.rotation.eulerAngles;
         Vector3 playerPos = new Vector3(transform.position.x, 1f, transform.position.z);
         transform.position = playerPos;
         if (Input.GetKey(KeyCode.Return)) {
             if (Cursor.lockState == CursorLockMode.Locked)
             {
                 Cursor.lockState = CursorLockMode.None;
             }
             else if (Cursor.lockState == CursorLockMode.None) {
                 Cursor.lockState = CursorLockMode.Locked;
             yield return new WaitForSeconds(0.1f);
 
             }
         }
     }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movement : MonoBehaviour {
     // Use this for initialization
     void Start () {
         Cursor.lockState = CursorLockMode.Locked;
     }
     
     // Update is called once per frame
     void **Update** () {
         float Horizontal = Input.GetAxis("Horizontal"), Vertical = Input.GetAxis("Vertical");//this gets the wasd and arrow key input
         transform.Translate(Horizontal * Time.deltaTime * 5, 0f, Vertical * Time.deltaTime * 5);//this moves the player
         float MouseX = Input.GetAxis("Mouse X"), MouseY = Input.GetAxis("Mouse Y");//gets the mouse positino
         transform.Rotate(-MouseY,MouseX,0);//rotates the player as they move the mouse
         Vector3 playerRot = transform.rotation.eulerAngles;
         Vector3 playerPos = new Vector3(transform.position.x, 1f, transform.position.z);
         transform.position = playerPos;
         if (Input.GetKey(KeyCode.Return)) {
             if (Cursor.lockState == CursorLockMode.Locked)
             {
                 Cursor.lockState = CursorLockMode.None;
             }
             else if (Cursor.lockState == CursorLockMode.None) {
                 Cursor.lockState = CursorLockMode.Locked;
             yield return new WaitForSeconds(0.1f);
 
             }
         }
     }
 }


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

1 Reply

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

Answer by MacDx · Nov 30, 2017 at 12:10 AM

You get that error because you are trying to use the yield statement inside update and that is not possible. Yield statements are reserved for IEnumerator methods (Coroutines, or iterators if you want to be more specific).

So if you want your code to yield for some time you will need to rethink your approach a little bit. The question is, why do you want to wait some time? what are you trying to achieve here? Despite that, the first change you need to do is get rid of that yield statement and put it inside a method with an IEnumerator as its return type.

Comment
Add comment · Show 3 · 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 Pagwin · Nov 30, 2017 at 10:44 PM 0
Share

Thanks for the answer I wanted to have the wait because whenever I would push enter(the button I set to change whether the cursor would be locked or not) sometimes it would toggle both ways which I wanted to stop with a wait so I could take my finger off the key fully and it wouldn't register it again

avatar image MacDx Pagwin · Dec 04, 2017 at 06:00 PM 0
Share

Ahh then the problem is that you're using Input.Get$$anonymous$$ey($$anonymous$$eyCode.Return) ins$$anonymous$$d of Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return). The different between that Get$$anonymous$$ey and Get$$anonymous$$eyDown is that Get$$anonymous$$ey will return true every frame that the key is pressed but Get$$anonymous$$eyDown will only return true the same frame the key was pressed.

avatar image Pagwin MacDx · Dec 04, 2017 at 08:39 PM 0
Share

thanks for the help

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

142 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 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 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 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 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 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

UnassignedReferenceException: The variable closest of TAKE has not been assigned. 0 Answers

Getting an error: Assertion failed on expression: 'SUCCEEDED(hr)' 5 Answers

Unity bug gives false error messages or doesen't work without a Debug.Log() line present 1 Answer

Problem while downloading Creator Kit- Beginner code 0 Answers

Sprite Sheet Issues 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