Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IEnumerator seems not to be recognized by the editor

Discussion in 'Scripting' started by jmaldonado93, Apr 13, 2014.

Thread Status:
Not open for further replies.
  1. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    I am using WaitForSeconds to dissapear an object and then to make it visible again and I noticed that IEnumerator seems not to be recognized in the editor. My code is shown below and, in the editor, the word "IEnumerator" appears in red and when I hover the mouse over it I get a tooltip saying "error CS0103: the name IEnumerator does not exist in the current context". However, the code runs fine. I thought that I had missed a using statement but "using System.Collections" is included and it seems IEnumerator is part of such a namespace. I use the default Mono Develop Unity3D editor. I will very much appreciate your comments.

    Code (csharp):
    1.  
    2. IEnumerator RespawnWaitTime()
    3.     {
    4.         this.renderer.enabled = false;
    5.         // respawnWaitTime has been instanciated in advanced.
    6.         yield return respawnWaitTime;
    7.         this.renderer.enabled = true;
    8.     }
    9.  
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You need to add the following line at the top of your script:
    Code (csharp):
    1.  
    2. using System.Collections;
    3.  
     
  3. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    That line is in my code already. There are 2 using statements at the beginning of my code as follows:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Can you post the whole script?
     
  5. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    Here is the complete code. In my editor the word "IEnumerator" appears in red and also, when I hover the mouse over it I see a tooltip with the following text: "error CS0103: the name IEnumerator does not exist in the current context". Nevertheless, the code executes correctly without any run-time error.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scriptEnemy : MonoBehaviour {
    6.     // ------------------------------
    7.     // Public variables.
    8.     // ------------------------------
    9.     public int intNumberOfClicks = 2;
    10.     WaitForSeconds fltRespawnWaitTime = new WaitForSeconds(2);
    11.     public Color[] shapeColor = new Color[5];
    12.     public Transform explosion;
    13.  
    14.     // ------------------------------
    15.     // Private variables.
    16.     // ------------------------------
    17.  
    18.     // -----------------------------------------------------------------------------------------
    19.     // Use this for initialization
    20.     // -----------------------------------------------------------------------------------------
    21.     void Start () {
    22.     }
    23.  
    24.     // -----------------------------------------------------------------------------------------
    25.     // Update is called once per frame
    26.     // -----------------------------------------------------------------------------------------
    27.     void Update () {
    28.         if (intNumberOfClicks <= 0)
    29.         {
    30.             if (explosion)
    31.             {
    32.                 this.Instantiate(explosion, transform.position, transform.rotation);
    33.             }
    34.             Vector3 v3Position = new Vector3(Random.Range(-4.8f, 4.8f), Random.Range(-3.5f, 3.5f), 0.0f);
    35.             StartCoroutine(RespawnWaitTime());
    36.             transform.position = v3Position;
    37.             intNumberOfClicks = 2;
    38.         }
    39.     }
    40.  
    41.     // -----------------------------------------------------------------------------------------
    42.     // Hide the object for an amount of time and then unhide it.
    43.     // -----------------------------------------------------------------------------------------
    44.     IEnumerator RespawnWaitTime()
    45.     {
    46.         this.renderer.enabled = false;
    47.  
    48.         // Not a good choice because everytime "new WaitForSeconds(fltRespawnWaitTime)" is executed
    49.         // there are 21 bytes of garbage allocation.
    50.         // The best practicce is to instanciate it in advance (see Public Variables above).
    51.         //yield return new WaitForSeconds(fltRespawnWaitTime);
    52.  
    53.         yield return fltRespawnWaitTime;
    54.         RandomColor ();
    55.         this.renderer.enabled = true;
    56.     }
    57.  
    58.     // -----------------------------------------------------------------------------------------
    59.     // Changes the color of an object randomly.
    60.     // -----------------------------------------------------------------------------------------
    61.     void RandomColor()
    62.     {
    63.             int newColor = Random.Range (0, 4);
    64.             renderer.material.color = shapeColor [newColor];
    65.     }
    66. }
    67.  
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Using your same script, I do not get the same error. Perhaps build a test project?
     
  7. mrlchristie

    mrlchristie

    Joined:
    Apr 11, 2014
    Posts:
    10
    You've probably solved this by now, but if not, I have had that problem before. I was using the latest .net framework and I went to Programs and Features within control panel (if you're using Windows) and selected the box to also run .net framework 3.5 I think it was.

    I then shut down my project after saving, restarted my machine and it worked fine.
     
  8. anhtran

    anhtran

    Joined:
    Jan 31, 2016
    Posts:
    1
    i have same error like that but i still ignore and it work
     
  9. Khudere

    Khudere

    Joined:
    Jun 7, 2014
    Posts:
    9
    I'm having this error show up and still able to run the project, still looking into why ManoDevelop is doing this
     
  10. ByomTheGAMEDEVGOD

    ByomTheGAMEDEVGOD

    Joined:
    Apr 1, 2022
    Posts:
    1
    i am suffering same error
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    And everyone is suffering from this super old thread being necroed. All that is happening is lack of understand here.

    If you have a problem, don't lazy-post and just say "same error"; create your own thread, post the details and maybe someone will help.

    Let's close this thread.
     
    Kurt-Dekker and Bunny83 like this.
Thread Status:
Not open for further replies.