名称IEnumerator在当前上下文中不存在

本文关键字:上下文 不存在 IEnumerator 名称 | 更新日期: 2023-09-27 18:02:36

这是一段时间以来我遇到的最恼人的错误。我想用c#在Unity中创建一个简单的循环,将摄像机移动到另一个点。

我正在使用System.Collections。通用",IEnumerator甚至出现在建议,当我开始输入它,但一旦我完成,它变成红色,并有一个错误,读"资产/脚本/NerworkManager.cs(190,9):错误CS0246:类型或命名空间名称' IEnumerator'找不到。您是否缺少using指令或程序集引用?",以及编辑器中的"错误CS0103:名称IEnumerator在当前上下文中不存在"。

下面是我的代码:

IEnumerator LerpCam(Camera c, Vector3 target, float length){
        float startTime = Time.time;
        while (Time.time < startTime + length) {
            c.transform.position = Vector3.Lerp (c.transform.position, target, Time.deltaTime);
        }
    yield return null;
}

我不知道问题是什么,并且能够使用通用集合中的其他东西而没有问题。

名称IEnumerator在当前上下文中不存在

IEnumeratorSystem.Collections中,IEnumerator<T>System.Collections.Generic中。所以要确保你有正确的配对

名称'Enumerable'不存在,因为它需要Linq,而Linq在Framework 4.0

不要忘记使用库:

using System.Collections;
using System.Linq;

你必须把

using System.Collections;

我添加了using System。集合和我仍然得到错误"名称'Enumerable'不存在于当前上下文中"。我添加了使用系统。Linq和错误消失