unity – “Update cannot be an iterator block” when trying to wait for jump to finish

When you want a method that can pause its execution and resume later in Unity, that’s called a Coroutine, and it needs to be set up in a specific way. The return type has to be IEnumerator, not void.

That’s why the compiler complains when you try to put a yield return statement inside a void Update – “void” is not a type that can be used as an iterator, which is what Unity’s coroutines are built on.

If you change the return type to IEnumerator, Unity will tell you that the Update method is forbidden from being used as a coroutine. But that’s OK, we can start our own named whatever we like.

Here’s a version of your script with working syntax:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    Animator m_Animator;
    bool m_Jump;

    public CharacterController2D controller;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;

    void Start()
    {
        m_Animator = gameObject.GetComponent<Animator>();

        // Start our coroutine when the MonoBehaviour activates.
        StartCoroutine(JumpPoll());
    }

    IEnumerator JumpPoll()
    {
        while(true) {
            // If you don't want to lock horizontal input while jumping,
            // then move this line into FixedUpdate. It's OK to read axes there.
            horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

            if (Input.GetButtonDown("Jump"))
            {
                jump = true;
                m_Animator.SetBool("Jump", true);

                // Pause this loop for 3 seconds while the rest of the game proceeds.
                yield return new WaitForSeconds(3);

                m_Animator.SetBool("Jump", false);
            }
            // Let the game run one frame, and resume next frame just like Update() does.
            yield return null;
        }
    }

    void FixedUpdate()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
        jump = false;
    }
}