My Unity Engine says there is an error when i use IEnumerator

im trying to make an interactive script to open doors in unity and when i try to use IEnumerator, it says “the type or namespace name ‘IEnumerator’ could not be found”. i cant seem to find a solution yet and so i thought i would ask about it online. The script is shown below

using UnityEngine;

public class Door : MonoBehaviour
{
    public GameObject door_closed, door_open;
    public bool open;

    void OnTriggerStay(Collider other)
    {
        if(other.CompareTag("Door Detect"))
        {
            if(Input.GetKeyDown(KeyCode.E))
            {
                door_closed.SetActive(false);
                door_open.SetActive(true);
                StartCoroutine(repeat());
                open = true;
            }
        }
    }
    IEnumerator stay(){
        yield return new WaitForSeconds(4.00f);
        open = false;
        door_closed.SetActive(true);
        door_open.SetActive(false);
    }
}

Put using System.Collections; at the top of your script along with the other one.

This used to be there by default but has recently been removed from the default script templates for whatever reason.

For an explanation, C# has an organisation system called namespaces that are used to organise types together: Organizing types in namespaces - C# | Microsoft Learn

IEnumerator belongs in the System.Collections namespace, and so it needs to be imported into this script to be able to use it.

1 Like