- Home /
Make GUI box only appear when I want it to, and disappear again afterwards.
I have a GUI textbox which appears when my game begins- the only problem is that it shouldn't. I have set up a boolean variable which is made true by hitting a trigger, and the GUIBox should only be drawn when I do. However, for some reason, the code just isn;t working.
Here's the code:
var showBox: boolean = false; var textAsset: TextAsset;
function OnTriggerEnter (other: Collider) { if (other.gameObject.GetComponent(CharacterController)){ showbox = true; } }
function OnGUI() { if (showBox == true) { GUI.Box(Rect((Screen.width/5),(Screen.height/5),((Screen.width/5)*3),((Screen.height/5)*3)), textAsset.text); } }
What am I doing wrong here? Thanks in advance.
P.S, I previously had if (showBox), but I changed it in a vague hope it would help.
Answer by DaveA · Apr 29, 2011 at 04:49 PM
Check the inspector to see if the default was overridden to true.
Answer by GlennHeckman · Apr 29, 2011 at 05:54 PM
You should also get in the habit of setting your variables (outside of functions) to either public or private. This will let the inspector know whether or not it can have control (or priority) of the variables value.
public var showBox:boolean = false; ----> Visible and editable in property panel
private var showBox:boolean = false; ----> Hidden from property panel (& other scripts)
Edit: I see that you are using the Screen dimensions to determine GUI Positioning. I'm not sure if you want it to be actively updating (react to window resize), but you should calculate the GUI's Rect() outside of the OnGUI function if possible ... you don't want to waste those precious calculations on redundant info! Just an observation ..
Your answer
Follow this Question
Related Questions
[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit? 1 Answer
Using a trigger to make a scrollview appear and then disappear? 1 Answer
Trigger collision with Player won't work 2 Answers
Help with making a triggered gui message 1 Answer
UI button doesn't appear - c# 3 Answers