How to add a duration for text? (roll a ball)

Hi, I am completely new to Unity and have been working on it at school. Of course, my start has been the “Roll a Ball” thing, and I’ve tried looking about to make some changes. I was wanting to change (or really just add in) the duration of the “winText” that pops up directing the player to move ahead. At the moment it just stays there permanently, but I’d like it to disappear after 5 seconds.

If someone had any idea how to do this it would be greatly appreciated.

This is what I had in total:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public float speed;
	public Text countText;
	public Text winText;

	private Rigidbody rb;
	private int count;

	void Start ()
	{
		rb = GetComponent<Rigidbody>();
		count = 0;
		SetCountText ();
		winText.text = "";
	}

	void FixedUpdate ()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

		rb.AddForce (movement * speed);
	}

	void OnTriggerEnter(Collider other) 
	{
		if (other.gameObject.CompareTag ("Pick Up"))
		{
			other.gameObject.SetActive (false);
			count = count + 1;
			SetCountText ();
		}
	}

	void SetCountText ()
	{
		countText.text = "Count: " + count.ToString ();
		if (count >= 8) 
		{
			winText.text = "Great. Now hit the blue wall.";
		}
	
	}

	void OnCollisionEnter (Collision col)
	{
		if (col.gameObject.name == "North Wall" && count >= 8)
        {
			Destroy (col.gameObject);
		}
	}   
}

Hi! So it looks like what will happen in what you coded is that the player collects eight token, a message comes up instructing the player to hit the blue (north) wall, and then upon that collision the wall is destroyed.

To actually change the duration of the message, I would create a new method and call it with a delay using the Invoke function. Mine would look something like this (replace your SetCountText function with this and add my method to your code as well):

void SetCountText ()
     {
         countText.text = "Count: " + count.ToString ();
         if (count >= 8) 
         {
             winText.text = "Great. Now hit the blue wall.";
             Invoke("ClearWinText", 5f);
         }
     
     }

void ClearWinText()
{
        winText.text = " ";
}

What this will do is as soon as the winText has its text set, it will invoke the method ClearWinText after 5 seconds (Invoke takes two arguments, the first being the string name of the method and the second being the delay in seconds before the method is executed). When ClearWinText is called, it will set the winText text back to just an empty string, as it was set to at the beginning. You can change the amount of time the message is active by changing that second argument (5f) in the invoke function.

Let me know if you have any questions and I’d be happy to answer!