Hi.
I was just wondering how to make a GUIText dissapear after a few seconds.
I have this script right now that triggers a GUIText when I walk over a triggered area.
var theGuiObject : GUIText; // set this in the inspector view
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
theGuiObject.enabled = true;
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
theGuiObject.enabled = false;
}
}
You’d want to handle the case of the player entering the trigger, leaving, then re-entering.
var theGuiObject : GUIText;
var displayTime = 3.0;
private var timerActive = false;
private var timer = 0.0;
function OnTriggerEnter (other : Collider) {
if (!other.CompareTag ("Player")) { // Do nothing if tag is not "Player"
return;
}
if (timerActive) { // Reset timer if player re-enters trigger while the timer's active
timer = 0.0;
return;
}
theGuiObject.enabled = true; // Enable GUIText, wait a while, then disable it
yield Wait();
theGuiObject.enabled = false;
}
function Wait () { // A custom WaitForSeconds coroutine that can be reset
timerActive = true;
timer = 0.0;
while (timer < displayTime) {
timer += Time.deltaTime;
yield;
}
timerActive = false;
}
i think you can use yeild WaitForSeconds
maybe like this :
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
theGuiObject.enabled = true;
WaitToDisappear();
}
}
function WaitToDisappear()
{
yield WaitForSeconds(//set your time in here)
theGuiObject.enabled = false;
}