c# - 如何解决 IEnumerator(Unity, C#) 的错误?

标签 c# unity3d ienumerator

我需要让我的角色跑墙,但是我的 IEnumerator 有代码问题

这是针对 Unity 4.5.x,用 C# 编写的代码

using UnityEngine;
using System.Collections;

public class Moving : MonoBehaviour {

public float speed = 6.0F;
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F;
public float runTime = 1.0f;
private Vector3 moveDirection = Vector3.zero;
private bool isWallL = false;
private bool isWallR = false;
private RaycastHit hitL;
private RaycastHit hitR;
private int jumpCount = 1;

IEnumerator afterRun() {
    yield return new WaitForSeconds (runTime);
    isWallL = false;
    isWallR = false;
    gravity = 20;
}
void Update() {
    CharacterController controller = GetComponent<CharacterController>();

    if (controller.isGrounded) {
        jumpCount = 0;
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }

    if (Input.GetKeyDown (KeyCode.Space) && !controller.isGrounded && jumpCount <= 1) {
        if (Physics.Raycast (transform.position, -transform.right, out hitL, 1)){
            if (hitL.transform.tag == "Wall"){
                isWallL = true;
                isWallR = false;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
        if (Physics.Raycast (transform.position, transform.right, out hitR, 1)){
            if (hitR.transform.tag == "Wall"){
                isWallL = false;
                isWallR = true;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
    }
}

预期没有错误,但我有两个:

error CS1502: The best overloaded method match for UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)' has some invalid arguments" and "error CS1503: Argument #1' cannot convert method group' expression to type System.Collections.IEnumerator'.

最佳答案

代码中的 afterRun 是一个函数,但您调用它时没有用括号。所以:

StartCoroutine (afterRun());

例如:

namespace someNamespace
{ 
    public class SomeClass
    {
        IEnumerator afterRun()
        {
            yield return new WaitForSeconds(3);            
        }

        public void Test(IEnumerator enumerator)
        {
            while(enumerator.MoveNext())
            {
                //do some work
            }
        }

        public void YoureCode()
        {
            Test(afterRun());
        }
    }

    public class WaitForSeconds
    {
        public WaitForSeconds(int a)
        {            
        }
    }
}

更多信息见 enter link description here

关于c# - 如何解决 IEnumerator(Unity, C#) 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57686367/

相关文章:

c# - 在oracle中使用 "nested"事务

c# - 如何终止递归函数并返回值

c# - Unity - 在游戏中添加背景音乐

c# - 从数组中获取通用枚举器

c# - IEnumerator 问题

c# - C#类型为 'System.Runtime.InteropServices.COMException'的未处理异常

android - Unity 4.3 2d 以编程方式创建 Sprite

c# - 淡出对象

c# - 如何为多个foreach实现正确的IEnumerator接口(interface)?

c# - 是否可以异步提交/回滚 SqlTransaction?