- Home /
GUIText dissapear after a few seconds
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;
}
}
Answer by Eric5h5 · Dec 30, 2012 at 08:44 PM
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;
}
ok, this is the true answer... $$anonymous$$ark it as true answer... i `m lose now :)
I guess this would work but the text is still there for me. I'm using this script for a battery pick-up. The trigger destroys after I walk in to the triggered area and then the text pops up. Can the problem be that the trigger destroys or what?
Don't destroy the trigger, because then the script is destroyed with it and the code can't run because it doesn't exist anymore.
But I just want to use the trigger once, not multiple times
Don't destroy it until after the script is no longer needed, then.
Answer by m4s4m0r1 · Dec 30, 2012 at 03:07 PM
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;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUIText not working properly with a timer. 1 Answer
Score System help 1 Answer
RPG Levelling Script using GUI Text help 0 Answers
Having problems with my timer 1 Answer