How to make script wait x seconds, then act?

I have this script, it’s for an idle animation. The conditions should be, the player is idling (grounded and move ==0) and then, if the player waits for 6 seconds, a random idle animation should play, and a random result should be printed on the console.
My problem is that after 6 seconds, when I move, then wait, the script does not wait 6 seconds before playing again, and gives me a constant array of results, but I wanted there only to be one result each 6 seconds. This is part of my script.

		//Checking for Idle Animation
		if (grounded == true && move == 0)
		{
			Debug.Log ("Player is now Idling");
			StartCoroutine(Idle());
			}
	}
	
	
	IEnumerator Idle ()
	{
		yield return new WaitForSeconds (6);
		var number = Random.Range (1, 4);
		if (number == 1)
		{
			Debug.Log ("Result = 1");
		}
		else if (number == 2)
		{
			Debug.Log ("Result = 2");
		}
		else if (number == 3)
		{
			Debug.Log ("Result = 3");
		}
		else if (number == 4)
		{
			Debug.Log ("Result = 4");
		};
	}

I use WaitForSeconds but this doesn’t work. Help?

maybe you could try to use the benefit of Invoke() method.

void Start(){
Invoke("doIt",10); // Wait for 10 seconds
}

void doIt(){
Debug.Log("After waiting 10 seconds");
}

and from your example, it would be similar like this :

         //Checking for Idle Animation
         if (grounded == true && move == 0)
         {
             Debug.Log ("Player is now Idling");
             Invoke("Idle",15);
             }
     }
     
     
     void Idle () 
     {
         var number = Random.Range (1, 4);
         if (number == 1)
         {
             Debug.Log ("Result = 1");
         }
         else if (number == 2)
         {
             Debug.Log ("Result = 2");
         }
         else if (number == 3)
         {
             Debug.Log ("Result = 3");
         }
         else if (number == 4)
         {
             Debug.Log ("Result = 4");
         };
     }

First of all, your script is a bit confusing. You showed that once you enter the idle state, you wait for 15 seconds and then pick a random number from 1 to 4. This has little to do with your question and problem.

But I’m going to explain my solution to your problem.

Make states for your character. The moment your character enters the Idle state from other states, you do StopAllCoroutines() and then start the counting Coroutine. The Coroutine counts 6 seconds and then starts the Idle animation.

if (lastState != idle && currentState == idle){  // Obviously, you have to declare/change the variables and states depending on the way you want to do this part.
    StopAllCoroutines();  // So that if you move before the 6 seconds finish, the counter won't keep counting.
    StartCoroutine(Idle());
}
else {
    StopAllCoroutines();  // So that the Idle animation won't randomly start playing if you move before the 6 seconds are up.
}

IEnumerator Idle(){
    yield return new WaitForSeconds(6);
    // Play Idle Animation or call a function that can do that.
}

I’m guessing the top snippet is in Update? In which case, you’re launching lots of concurrent coroutines - one in every frame in which you’re idling. I’d suggest you implement this a different way not using coroutines at all, but using a timer to record the last time at which input was received. If currentTime - timeLastInputReceived > 6, play the animation.