WaitForSeconds
I am trying to code a game in C# where if you collect something, text will show for 10 seconds and then disappear. I am trying to use WaitForSeconds, but when i collect the object, the text just stays on the screen. Here is my code.`using UnityEngine; using UnityEngine.UI; using System.Collections;
public class CoordinatingConjunctionText : MonoBehaviour {
public Text CcText;
// Use this for initialization
void Start () {
CcText.text = "";
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Player"))
{
CcText.text = "COORDINTATING CONJUNCTION" +
"\n-Most common of 3 types" +
"\n-Joins words, phrases, and clauses together" +
"\n-Goes between the words that are being linked" +
"\nEx. Apples AND Cheez-Its are my favorite snacks." +
"\nEx. I had to climb the mountain, explore the cave, OR scuba dive." +
"\nEx. How he sings AND how he talks are very different.";
WaitAndCloseText ();
}
}
IEnumerator WaitAndCloseText ()
{
yield return new WaitForSeconds(10);
%|-436420141_29|% } } `
I'm sorry that not all the code is there, the system did something funny. Here is a picture of all of it.
Answer by hexagonius · Feb 14, 2016 at 06:30 PM
Instead of
WaitAndCloseText ();
use
StartCoroutine (WaitAndCloseText ());
@legoguy_1 That's odd. It should work. I took a step further and wrote a mocked up version of your script and it works perfectly.
In your OnTriggerEnter function, you wrote this: WaitAndCloseText ();
Replace that with StartCoroutine( WaitAndCloseText() );
.
It still won't work. That's so weird. I even deleted the script and re wrote it. Is the script supposed to be linked to the collectible because that is what i have done.
Your answer
Follow this Question
Related Questions
c# shooting problem (probably pretty simple) 2 Answers
How to interrupt wait coroutine in a loop? 0 Answers
IENumerator does not work 0 Answers
StartCoroutine not listening to parameters 1 Answer
Help with changing bool over time to play animation 1 Answer