- Home /
Two IF statements in a OnGUI function problem
I have two if statements that I have in a single OnGUI function and it seems that they cause many errors in script. Im not sure if I made a syntax error by having two if statements or if I just didnt put them in the function correctly.
#pragma strict
var triggered: boolean;
var width = Screen.width /2 - 100;
var height = Screen.height / 2 - 125 ;
var store : boolean;
function OnTriggerEnter() {
triggered = true;
}
function OnTriggerExit(){
triggered = false;
store = false;
}
function OnGUI() {
if (triggered) {
var textWidth = Screen.width / 2 - 75;
var textHeight = Screen.height / 2 - 10;
GUI.Label (Rect (textWidth, textHeight, 150, 20), "(E) Enter Store");
}
// if (store) {
// triggered = false;
//
// GUI.Box(Rect(width, height, 200, 250), "What do you need?");
//
// if (GUI.Button(Rect(width + 50,height + 90,100,30),"Buy Store"))
// //This will disable the store menu and produce the buy or offer menu to buy the store
//
// if (GUI.Button(Rect(width + 50,height + 120,100,30),"Apply for Work"))
// //this will disable the store menu and produce the application menu for work
//
// if (GUI.Button(Rect(width + 50,height + 150,100,30),"Shop for Items"))
// //This will disable the store menu and produce the shopping menu
//
// if (GUI.Button(Rect(width + 50,height + 180,100,30),"Nevermind"))
// //this will diable the store menu and allow the player to move
// }
}
function Update(){
if(triggered && Input.GetButtonUp ("e")){
store = true;
}
}
Can you post the errors it generates ins$$anonymous$$d of commenting out the code
there is many but the root problem is a missing "}" on line 48 but I see one there already
Answer by robertbu · Jun 05, 2013 at 04:39 AM
Your 'if' structure here is wrong (commented out code) . Plus nested 'if(GUI.Button())' statements don't work. GUI.Button() only returns true for a single frame, so there is no way to see or interact with nested calls. You could use GUI.RepeateButton(), but you could never use the nested buttons because the moment you lift your button to press on the new buttons, they would disappear. You can have multiple 'if (GUI.Button())' statements at the top level. In order to sort these kinds of problems out, I recommend always using brackets '{}' for every 'if' and 'else' statement. Here is your code edited so that it will compile:
#pragma strict
var triggered: boolean;
var width = Screen.width /2 - 100;
var height = Screen.height / 2 - 125 ;
var store : boolean;
function OnTriggerEnter() {
triggered = true;
}
function OnTriggerExit(){
triggered = false;
store = false;
}
function OnGUI() {
if (triggered) {
var textWidth = Screen.width / 2 - 75;
var textHeight = Screen.height / 2 - 10;
GUI.Label (Rect (textWidth, textHeight, 150, 20), "(E) Enter Store");
}
if (store) {
triggered = false;
GUI.Box(Rect(width, height, 200, 250), "What do you need?");
if (GUI.Button(Rect(width + 50,height + 90,100,30),"Buy Store")) {
//This will disable the store menu and produce the buy or offer menu to buy the store
}
if (GUI.Button(Rect(width + 50,height + 120,100,30),"Apply for Work")) {
//this will disable the store menu and produce the application menu for work
}
if (GUI.Button(Rect(width + 50,height + 150,100,30),"Shop for Items")) {
//This will disable the store menu and produce the shopping menu
}
if (GUI.Button(Rect(width + 50,height + 180,100,30),"Nevermind")) {
//this will diable the store menu and allow the player to move
}
}
}
function Update(){
if(triggered && Input.GetButtonUp ("e")){
store = true;
}
}
im not seeing any difference besides the added curly brackets in the code. I tried to compile but it states that in line 10 there needs to be a closing "}". How do I do the get the script to work inside the GUI without nesting if statements?
The script above compiles without errors, so if you are getting an error, you did not copy and paste correctly. As for "get the script to work," I cannot tell you exactly since I don't know your intended behavior. If you want one GUI.Button() to control other GUI elements, you will need to add a boolean value. Here is a simple example:
#pragma strict
var show = false;
var width = Screen.width /2 - 100;
var height = Screen.height / 2 - 125 ;
function OnGUI() {
if (GUI.Button(Rect(width + 50,height + 90,150,30),"Show/Hide Other")) {
show = !show;
}
if (show) {
if (GUI.Button(Rect(width + 50,height + 120,150,30),"Other")) {
// Do Other stuff
}
}
}
Your answer

Follow this Question
Related Questions
Event.type check doesnt work inside nested list 0 Answers
Variables in GUI not updating/changing? (javascript) 1 Answer
Using Time with OnGUI Help 2 Answers
I need some help on inventory. 0 Answers