WaitForSeconds troubles

I have a nav mesh agent that collides with an object and destroys the other object and I want it to wait a few seconds before returning to its spawn.

IEnumerator crimeWait()
	{
        print ("should be waiting");
		yield return new WaitForSeconds (3);
	}

	void OnTriggerEnter(Collider other) 
	{
		if (other.tag == "crime")
		{
			Destroy(other.gameObject);
			StartCoroutine (crimeWait());
			randomInstance.crimeCount--;	
			destination = GameObject.FindGameObjectWithTag("ReturnPoint").GetComponent<Transform>();
			complete = true;			
			randomInstance.money+=100;
		}

This doesn’t seem to be working and I’m not sure why. I get should be waiting in the console but the agent doesn’t stop at all. Any help is appreciated, thanks :slight_smile:

After IEnumerator yields, it executes statement after the yield statement.
The OnTriggerEnter function keeps executing the lines of the method irrespective of what is happening in the Coroutine.
Think of coroutine as if it is working in parallel to the main code. Whatsoever happens in the code, remain unaffected by what is happening in the coroutine.

    IEnumerator OnTriggerEnter(Collider other)
    {
         if (other.tag == "crime")
         {
             Destroy(other.gameObject);
             yield return new WaitForSeconds(3);
             randomInstance.crimeCount--;    
             destination = GameObject.FindGameObjectWithTag("ReturnPoint").GetComponent<Transform>();
             complete = true;            
             randomInstance.money+=100;
         }

Instead of calling function/method OnTriggerEnter, call the IEnumerator by

StartCoroutine (OnTriggerEnter(other));

IEnumerator crimeWait()
{
print (“should be waiting”);
yield return new WaitForSeconds (3);

randomInstance.crimeCount–;
destination = GameObject.FindGameObjectWithTag(“ReturnPoint”).GetComponent();
complete = true;
randomInstance.money+=100;
}

void OnTriggerEnter(Collider other)
{
if (other.tag == “crime”)
{
Destroy(other.gameObject);
StartCoroutine (crimeWait());

}}

I have not tested it but hope this will work as the code you want to execute after the wait must be called in the couroutine