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);
}
}
}
}
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.
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
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.
Your answer
Follow this Question
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