Your “Test” coroutine is a method. You can’t declare methods inside methods. It has to be declared inside your class. Also “Test” is a bad name. You should use a descriptive name like this:
using UnityEngine;
using System.Collections;
public class Keyandlock : MonoBehaviour
{
public GameObject Key;
public GameObject ChestLid;
public GameObject ChestOpen;
public GameObject Chestanim;
public GameObject Open;
public GameObject ChestContence;
void Start ()
{
ChestLid.SetActive (true);
ChestOpen.SetActive (false);
Chestanim.SetActive (false);
Open.SetActive (false);
ChestContence.SetActive (false);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Key")
{
Debug.Log ("Detected");
ChestContence.SetActive (true);
Key.SetActive (true);
ChestLid.SetActive (false);
Open.SetActive (true);
//Animation
Chestanim.SetActive (true);
StartCoroutine(OpenChestDelayed());
}
}
IEnumerator OpenChestDelayed()
{
yield return new WaitForSeconds (1);
ChestOpen.SetActive (true);
}
}