- Home /
how to script a timed GUI Label into my script
Hey guys,
I have this script so far, and what i want to do is make a timed GUI Label, so that when the player comes into range of the "objective" variable (this script is attached to the player) then it displays a GUI Label that only lasts for a couple of seconds....
var objective : Transform;
function OnGUI{
if (Vector3.Distance(transform.position, objective.position) < 3000){
}
}
I have tried using the yield method (http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html), but that doesn't seem to work entirely...
any answers would be greatly appreciated!!!!
-Grady
Answer by GuyTidhar · Jul 01, 2011 at 09:21 AM
var objective : Transform;
var showLabel : boolean = false;
var timeUntilHide : float;
function Update()
{
if (Vector3.Distance(transform.position, objective.position) < 3000 )
{
if ( !showLabel )
StartCotourine("ShowAndWaitUntilHide");
}
else if ( showLabel )
{
StopCoroutine("ShowAndWaitUntilHide");
showLabel = false;
}
}
function OnGUI
{
if ( showLabel )
GUI.Label(Rect(10,10,20,20), "MyText");
}
function ShowAndWaitUntilHide()
{
timeUntilHide = 5;
showLabel = true;
while ( timeUntilHide > 0 )
{
// Wait 1 frame before returning to this loop
yield;
timeUntilHide -= Time.deltaTime;
}
showLabel = false;
}
i tested the script above, and the gui text appears when i get into within 3000 of the objective, but it doesn't dissapear???? how would i make it dissapear after about 5 seconds????
thanks
-Grady
I've changed the script. Also - make sure you do not set showLabel to false in another location as here.
i don't know if i've done something wrong, but it all works fine, until i get in range of the target. when i get in range, it appears fine, but after the 5 seconds it will flicker, and then reappear, and repeat... it will stay for another 5 seconds and then flicker???? i've tried having the "Show Label" variable ticked and unticked in the inspector, as well as manually entering the "Time Until Hide" variable
-Grady
Did you copy the new script? I've made a big change here...
Your answer
Follow this Question
Related Questions
GUI in the middle of the screen 1 Answer
GUI Label script problem??? 1 Answer
Creating a component from a button click 1 Answer
Get variable from another object 1 Answer
disable code when level is rendered 1 Answer