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.
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 ..
P.S, I previously had if (showBox), but I changed it in a vague hope it would help.
– anon38117566