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.
IEnumerator RespawnWaitTime()
{
this.renderer.enabled = false;
// respawnWaitTime has been instanciated in advanced.
yield return respawnWaitTime;
this.renderer.enabled = true;
}
You need to add the following line at the top of your script:
using System.Collections;
That line is in my code already. There are 2 using statements at the beginning of my code as follows:
using UnityEngine;
using System.Collections;
Can you post the whole script?
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.
using UnityEngine;
using System.Collections;
public class scriptEnemy : MonoBehaviour {
// ------------------------------
// Public variables.
// ------------------------------
public int intNumberOfClicks = 2;
WaitForSeconds fltRespawnWaitTime = new WaitForSeconds(2);
public Color[] shapeColor = new Color[5];
public Transform explosion;
// ------------------------------
// Private variables.
// ------------------------------
// -----------------------------------------------------------------------------------------
// Use this for initialization
// -----------------------------------------------------------------------------------------
void Start () {
}
// -----------------------------------------------------------------------------------------
// Update is called once per frame
// -----------------------------------------------------------------------------------------
void Update () {
if (intNumberOfClicks <= 0)
{
if (explosion)
{
this.Instantiate(explosion, transform.position, transform.rotation);
}
Vector3 v3Position = new Vector3(Random.Range(-4.8f, 4.8f), Random.Range(-3.5f, 3.5f), 0.0f);
StartCoroutine(RespawnWaitTime());
transform.position = v3Position;
intNumberOfClicks = 2;
}
}
// -----------------------------------------------------------------------------------------
// Hide the object for an amount of time and then unhide it.
// -----------------------------------------------------------------------------------------
IEnumerator RespawnWaitTime()
{
this.renderer.enabled = false;
// Not a good choice because everytime "new WaitForSeconds(fltRespawnWaitTime)" is executed
// there are 21 bytes of garbage allocation.
// The best practicce is to instanciate it in advance (see Public Variables above).
//yield return new WaitForSeconds(fltRespawnWaitTime);
yield return fltRespawnWaitTime;
RandomColor ();
this.renderer.enabled = true;
}
// -----------------------------------------------------------------------------------------
// Changes the color of an object randomly.
// -----------------------------------------------------------------------------------------
void RandomColor()
{
int newColor = Random.Range (0, 4);
renderer.material.color = shapeColor [newColor];
}
}
Using your same script, I do not get the same error. Perhaps build a test project?
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.
i have same error like that but i still ignore and it work
I’m having this error show up and still able to run the project, still looking into why ManoDevelop is doing this
i am suffering same error
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.
2 Likes