Deactivating a UI Text

So, I am very new to Unity and C# Scripting, I really don’t want to have to get into JavaScript If I have to, so if the answers below can please be using C# That would be fantastic.
I’m trying to make it so my UI Text of “You have completed your task” (WinText)to fade away after a set amount of time, Or just deactivate after some time, Preferably 3-5 seconds. I’m not sure how to do this, I’ve checked into Coroutines but I kind of understand it, but have no idea how to put it into my script. Take a look and tell me if you have any ideas! Thanks!
[My code Is set to Only display the UI Text as soon as Count reaches 100]


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

public class Player : MonoBehaviour {

private int count;
public Text countText;
public Text winText;
bool collected = false;

// Use this for initialization
void Start () {
count = 0;
SetCountText ();
winText.text = “”;
}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter ( Collider other ) {
if (other.gameObject.CompareTag (“Pick Up”)) {
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
if (count >= 100 && collected == false) {
GameObject gem = (GameObject)Instantiate(Resources.Load(“Gem”));
collected = true;
}
}

void SetCountText(){
countText.text = ": " + count.ToString ();
if (count >= 100) {
winText.text = “You have Completed your Task”;
}
}
}


Here is the easiest way to do it as I see it.

void SetCountText(){
    countText.text = ": " + count.ToString ();
    if (count >= 100) {
        winText.text = "You have Completed your Task";
        Invoke ("HideText",5);
    }
}

void HideText (){
    winText.SetActive(false);
}
1 Like

Alright I’ll try it out, If it works that will help me out on so many many many issues!

Im pretty sure I did this right,

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

public class Player : MonoBehaviour {

private int count;
public Text countText;
public Text winText;
bool collected = false;

// Use this for initialization
void Start () {
count = 0;
SetCountText ();
winText.text = “”;
}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter ( Collider other ) {
if (other.gameObject.CompareTag (“Pick Up”)) {
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
if (count >= 100 && collected == false) {
GameObject gem = (GameObject)Instantiate(Resources.Load(“Gem”));
collected = true;
}
}

void SetCountText(){
countText.text = ": " + count.ToString ();
if (count >= 100) {
winText.text = “You have Completed your Task”;
Invoke (“HifeText”, 5);
}
}

void HideText (){
winText.SetActive (false);
}
}

But I get an error message that says, Unity Engine does not contain definition for SetActive

You should use code tags and post the whole error message.

You may need to do winText.gameObject.SetActive(false); instead.