WaitForSeconds not working

Hello unityAnswers community, I’m facing a problem with a code I made in my game: The game is about a rolling ball (basically like the MarbleBlast serie by GarageGames), that works exactly the same as that but with new things I will add. After months of programming (I’m still a C# beginner), I got up to the part where I have to program an appearing (by triggers) and disappearing after 5 seconds (with WaitForSeconds) script. The problem is that I don’t know how to make this work. Following are some snippets of my code and what I tried:

The first thing I tried was this:

void OnTriggerEnter(Collider other)
 	{
 if(other.gameObject.tag == "TTrigger1")
 			{
 				Text1.text = "Trigger 1 is working properly";
 			Debug.Log("Trigger1Activated");
 			FiveSec();
				Text1.text = "";
 			Debug.Log("Trigger1Deactivated");
 
 			}
IEnumerator FiveSec() {
 		yield return new WaitForSeconds(5);
 	}
 	
	}

But this doesn’t work because if there’s FiveSec() in the trigger script, the code ignores it and goes straight to making Text1 blank (Verified with Debug.Log)

I also tried putting the trigger code in the IEnumerator code, leaving only FiveSec() there.
Also I can’t write the WaitForSeconds code because it has to be in an IEnumerator function. How do I correctly call WaitForSeconds in a void function? Thanks in advance

To start the coroutine:

StartCoroutine(FiveSec()); //you wont be able to stop it if you pass the method
//or
StartCoroutine("FiveSec");
//and to stop it
StopCoroutine("FiveSec");

You have a couple of problems here. First, Coroutines don’t work like you are trying to use them here. A coroutine executes until it hits a yield, and then it returns control to the line after it is called. So the line after you call the Coroutine is run immediately, not in five seconds. Second, you are not using StartCoroutine() (as @YoungDeveloper has pointed out). So for example, if you were to add the StartCoroutine() and then put a Debug.Log() statement just after the ‘yield’, you would see the output from the Debug.Log() would wait for five seconds after the collider was triggered.

One solution is to first add the StartCoroutine(), then you need to move the code that you are using to display the text into the FiveSec() coroutine.

IEnumerator FiveSec() {
    Text1.text = "Trigger 1 is working properly";
    yield return new WaitForSeconds(5);
    Text1.text = "";
}

As an alternate solution, OnTriggerEnter can be a coroutine. Just change the declaration to:

 IEnumerator OnTriggerEnter(Collider other)

Then you can put the yield directly in OnTriggerEnter().