- Home /
Gui text script - help
So i wrote a script that should appear a gui text and Ontriggerenter it should change it , then Ontriggerexit it should destroy the game object ... the script works perfect but i want it to show my GUIbox with the text ONLY after "OnTriggerEnter" , but i cant combine OnGui function and OnTriggerEnter function ! Can anyone help me please ?
var labelText : String = "";
function OnGUI() {
GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
}
function OnTriggerEnter( other : Collider ){
labelText = "";
}
function OnTriggerExit( other : Collider ){
Destroy(gameObject);
}
I thought about an variation like this :
function OnTriggerEnter( other : Collider ){ GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText)); }
But it doesent work =(
Answer by Nercoe · Nov 11, 2012 at 03:53 PM
try this:
var labelText : String = "";
var hasCollided : boolean = false;
function OnGUI() {
if (hasCollided ==true)
{
GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
}
}
function OnTriggerEnter( other : Collider ){
hasCollided = true;
labelText = "";
}
function OnTriggerExit( other : Collider ){
Destroy(gameObject);
}
The boolean declares whether or not the box has been collided with. If the box hasn't been collided with, set to false (on startup) if it has been collided with set the boolean to true and print the GUI.