The name IEnumerator does not exist in current context

17,976

Solution 1

IEnumerator is in System.Collections and IEnumerator<T> is in System.Collections.Generic. So make sure you have the correct matching pair.

Solution 2

The name 'Enumerable' does not exist because It requires Linq which is available at Framework 4.0

And don't forget to use the libraries:

using System.Collections;
using System.Linq;

Solution 3

You have to put

using System.Collections;

at the top of your file.

Solution 4

I added using System.Collections and I was still getting error "The name 'Enumerable' does not exist in the current context". I added using System.Linq and the error disappeared.

Share:
17,976
Var14ble
Author by

Var14ble

Updated on June 15, 2022

Comments

  • Var14ble
    Var14ble almost 2 years

    This is the most annoying error I've had in a while. I want to make a simple loop to move my camera to another point in Unity, using C#.

    I'm "using System.Collections.Generic", and IEnumerator even shows up in the suggestions when I start typing it, but as soon as I'm done, it goes red and has an error that reads "Assets/Scripts/NerworkManager.cs(190,9): error CS0246: The type or namespace name `IEnumerator' could not be found. Are you missing a using directive or an assembly reference?" in the console, and "error CS0103: The name IEnumerator does not exist in the current context" in the editor.

    Here's my code:

    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;
    }
    

    I have no idea what the problem is, and am able to use other things from the Generic collection without problems. Any help would be greatly appreciated.

    • juharr
      juharr almost 9 years
      IEnumerator is the non-generic interface and resides in System.Collections.
  • Var14ble
    Var14ble almost 9 years
    Thanks for that, can't believe I messed up something so simple, every other place was telling me I needed System.Collections.Generic... go figure.
  • jsanalytics
    jsanalytics almost 9 years
    Glad to help ! Cheers.
  • GGG
    GGG over 7 years
    Note that Enumerator = System.Linq.Enumerator and IEnumerator = System.Collections.IEnumerator/IEnumerator<T> = System.Collections.Generic.IEnumerator<T>. Enumerator and IEnumerator are not the same thing. One is the interface and the other is the implementation.
  • hookenz
    hookenz almost 4 years
    The using System.Linq worked for me, but the accepted answer didn't for some reason.