- Home /
I'm getting really mixed up on if statements! Too many! Help!
I have these two scripts that when opened shows an inventory with two tabs. If you click on the crafting menu tab then you will go to the crafting menu and it disables the inventory and same ,but the opposite way around if you then click on the inventory tab. You use tab to open and close the inventory and when you click on tab the second time what should happen is all the menus and tabs should close ,but what happens is the tabs close and the inventory stays there. I don't know whats wrong. Please make my if statements neater and fix what is happening.
// Here are then two scripts I am using:
using UnityEngine;
using System.Collections;
public class craftingMenu : MonoBehaviour
{
//PUBLIC THINGS
public static bool craftingMenuOn = false;
private bool craftingTabOn = false;
public Texture2D craftingWindow;
//CRAFTING WINDOW SIZE
public Vector2 craftingWindowPosition = new Vector2(0,0);
public Vector2 craftingWindowSize = new Vector2(800,800);
//GUISTYLES
public GUIStyle craftingTabStyle;
public GUIStyle craftingLabels;
public GUIStyle craftingButton;
void Start ()
{
}
void Update()
{
//CRAFTING MENU ON AND OFF
if (Input.GetKeyDown (KeyCode.Tab))
{
if (craftingTabOn == false)
{
craftingTabOn = true;
}
else if (craftingTabOn == true)
{
craftingTabOn = false;
inventorySystem.inventoryOn = false;
}
if(craftingMenuOn == false)
{
craftingMenuOn = false;
}
else if(craftingMenuOn == true)
{
craftingMenuOn = false;
inventorySystem.inventoryOn = false;
}
}
if (craftingMenuOn == true)
{
inventorySystem.inventoryOn = false;
}
}
void OnGUI()
{
if (craftingTabOn == true)
{
if (GUI.Button (new Rect (355, -25, 200, 200), "Crafting", craftingTabStyle))
{
craftingMenuOn = true;
inventorySystem.inventoryOn = false;
}
}
if (craftingMenuOn == true)
{
GUI.BeginGroup(new Rect(craftingWindowPosition.x,craftingWindowPosition.y,craftingWindowSize.x,craftingWindowSize.y),craftingWindow);
GUI.EndGroup();
}
}
}
//NEW SCRIPT STARTS RIGHT HERE. SORRY WHEN ADDING THE CODE SOMETHING MESSED UP...OR MAYBE IM JUST STUPID
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class inventorySystem : MonoBehaviour
{
//PUBLIC VARIABLES
public static bool inventoryOn = false;
private bool inventoryTabOn = false;
public Texture inventoryWindow;
//GUI FONT,SIZE, STYLE, AND LAYOUT
public Vector2 windowPosition = new Vector2(0,0);
public Vector2 windowSize = new Vector2(800,800);
public GUIStyle inventorySlotStyle;
public GUIStyle inventoryLabelStyle;
public GUIStyle inventoryTabStyle;
//MAX WEIGHT
public int weiLimit = 150;
public int curWei = 0;
//DICTIONARY
private Dictionary<int,string> inventoryDictionary;
void Start ()
{
inventoryDictionary = new Dictionary<int, string>()
{
//THE SLOTS FOR THE QUICK BAR
{0, string.Empty},
{1, string.Empty},
{2, string.Empty},
{3, string.Empty},
{4, string.Empty},
{5, string.Empty},
//MAIN INVENTORY SLOTS
{6, string.Empty},
{7, string.Empty},
{8, string.Empty},
{9, string.Empty},
{10, string.Empty},
{11, string.Empty},
{12, string.Empty},
{13, string.Empty},
{14, string.Empty},
{15, string.Empty},
{16, string.Empty},
{17, string.Empty},
{18, string.Empty},
{19, string.Empty},
{20, string.Empty},
{21, string.Empty},
{22, string.Empty},
{23, string.Empty},
{24, string.Empty},
{25, string.Empty},
{26, string.Empty},
{27, string.Empty},
{28, string.Empty},
{29, string.Empty},
//ARMOR SLOTS
{30, string.Empty},
{31, string.Empty},
{32, string.Empty},
{33, string.Empty}
};
}
void Update ()
{
//INVENTORY ON AND OFF
if (Input.GetKeyUp (KeyCode.Tab))
{
if (inventoryTabOn == false)
{
inventoryTabOn = true;
}
else if (inventoryTabOn == true)
{
inventoryTabOn = false;
craftingMenu.craftingMenuOn = false;
}
if (inventoryOn == false && craftingMenu.craftingMenuOn == false)
{
inventoryOn = true;
}
else if (inventoryOn == true && craftingMenu.craftingMenuOn == true)
{
inventoryOn = false;
craftingMenu.craftingMenuOn = false;
}
if(craftingMenu.craftingMenuOn == true && inventoryOn == false)
{
inventoryOn = false;
craftingMenu.craftingMenuOn = true;
}
}
}
void OnGUI()
{
//INVENTORY
//INVENTORY QUICK SELECT BAR
//SLOT 1
GUI.Button (new Rect(489,516,78,78),inventoryDictionary[0], inventorySlotStyle);
GUI.Label (new Rect (491,518,78,78),"1", inventoryLabelStyle);
//SLOT 2
GUI.Button (new Rect(579,516,78,78),inventoryDictionary[1], inventorySlotStyle);
GUI.Label (new Rect(583,518,78,78),"2", inventoryLabelStyle);
//SLOT 3
GUI.Button (new Rect(667,516,78,78),inventoryDictionary[2], inventorySlotStyle);
GUI.Label (new Rect(671,518,78,78),"3", inventoryLabelStyle);
//SLOT 4
GUI.Button (new Rect(755,516,78,78),inventoryDictionary[3], inventorySlotStyle);
GUI.Label (new Rect(759,518,78,78),"4", inventoryLabelStyle);
//SLOT 5
GUI.Button (new Rect(843,516,78,78),inventoryDictionary[4], inventorySlotStyle);
GUI.Label (new Rect(847,518,78,78),"5", inventoryLabelStyle);
//SLOT 6
GUI.Button (new Rect(931,516,78,78),inventoryDictionary[5], inventorySlotStyle);
GUI.Label (new Rect(935,518,78,78),"6", inventoryLabelStyle);
//TAB ON
if (inventoryTabOn == true)
{
if(GUI.Button(new Rect(194,-25,200,200),"Inventory", inventoryTabStyle))
{
inventoryOn = true;
craftingMenu.craftingMenuOn = false;
}
}
//INENTORY ON
if (inventoryOn == true)
{
//INVENTORY WINDOW
GUI.BeginGroup (new Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),inventoryWindow);
//INVENTORY SLOTS
//ROW ONE
GUI.Button (new Rect(291,64,78,78),inventoryDictionary[6], inventorySlotStyle);
GUI.Button (new Rect(379,64,78,78),inventoryDictionary[7], inventorySlotStyle);
GUI.Button (new Rect(467,64,78,78),inventoryDictionary[8], inventorySlotStyle);
GUI.Button (new Rect(555,64,78,78),inventoryDictionary[9], inventorySlotStyle);
GUI.Button (new Rect(643,64,78,78),inventoryDictionary[10], inventorySlotStyle);
GUI.Button (new Rect(731,64,78,78),inventoryDictionary[11], inventorySlotStyle);
//ROW TWO
GUI.Button (new Rect(291,152,78,78),inventoryDictionary[12], inventorySlotStyle);
GUI.Button (new Rect(379,152,78,78),inventoryDictionary[13], inventorySlotStyle);
GUI.Button (new Rect(467,152,78,78),inventoryDictionary[14], inventorySlotStyle);
GUI.Button (new Rect(555,152,78,78),inventoryDictionary[15], inventorySlotStyle);
GUI.Button (new Rect(643,152,78,78),inventoryDictionary[16], inventorySlotStyle);
GUI.Button (new Rect(731,152,78,78),inventoryDictionary[17], inventorySlotStyle);
//ROW FOUR
GUI.Button (new Rect(291,240,78,78),inventoryDictionary[18], inventorySlotStyle);
GUI.Button (new Rect(379,240,78,78),inventoryDictionary[19], inventorySlotStyle);
GUI.Button (new Rect(467,240,78,78),inventoryDictionary[20], inventorySlotStyle);
GUI.Button (new Rect(555,240,78,78),inventoryDictionary[21], inventorySlotStyle);
GUI.Button (new Rect(643,240,78,78),inventoryDictionary[22], inventorySlotStyle);
GUI.Button (new Rect(731,240,78,78),inventoryDictionary[23], inventorySlotStyle);
//ROW FIVE
GUI.Button (new Rect(291,328,78,78),inventoryDictionary[24], inventorySlotStyle);
GUI.Button (new Rect(379,328,78,78),inventoryDictionary[25], inventorySlotStyle);
GUI.Button (new Rect(467,328,78,78),inventoryDictionary[26], inventorySlotStyle);
GUI.Button (new Rect(555,328,78,78),inventoryDictionary[27], inventorySlotStyle);
GUI.Button (new Rect(643,328,78,78),inventoryDictionary[28], inventorySlotStyle);
GUI.Button (new Rect(731,328,78,78),inventoryDictionary[29], inventorySlotStyle);
//EQUIP SLOTS
//HEAD
GUI.Button (new Rect(42,64,78,78),inventoryDictionary[30], inventorySlotStyle);
GUI.Label (new Rect(46,68,75,75),"Head", inventoryLabelStyle);
//TORSO
GUI.Button (new Rect(42,152,78,78),inventoryDictionary[31], inventorySlotStyle);
GUI.Label (new Rect(46,156,75,75),"Chest", inventoryLabelStyle);
//LEGS
GUI.Button (new Rect(42,240,78,78),inventoryDictionary[32], inventorySlotStyle);
GUI.Label (new Rect(46,244,75,75),"Legs", inventoryLabelStyle);
//FEET
GUI.Button (new Rect(42,328,78,78),inventoryDictionary[33], inventorySlotStyle);
GUI.Label (new Rect(46,332,75,75),"Feet", inventoryLabelStyle);
//WEIGHT
GUI.Label(new Rect(731,407,78, 25), "Weight: " + curWei + " / " + weiLimit, inventoryLabelStyle);
GUI.EndGroup ();
}
}
}
don't have the time to check all the lines, but you might want to take a look at
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Tab)) {
if (craftingTabOn == false) {
craftingTabOn = true;
} else if (craftingTabOn == true) {
craftingTabOn = false;
inventorySystem.inventoryOn = false;
}
if(crafting$$anonymous$$enuOn == false) {
crafting$$anonymous$$enuOn = false;
} else if(crafting$$anonymous$$enuOn == true) {
crafting$$anonymous$$enuOn = false;
inventorySystem.inventoryOn = false;
}
}
since that code takes care of handling the Tab key, you would want to check the code and make sure that it also takes care of hiding the inventory..
also, are you sure that this is correct?:
if(crafting$$anonymous$$enuOn == false) {
crafting$$anonymous$$enuOn = false;
}
and that it shouldn't be
if(crafting$$anonymous$$enuOn == false) {
crafting$$anonymous$$enuOn = true;
}
I'll go over the code and comment on it after a few hours if no one else has done that by the time..
man thats a lot of if statements. As you get better with coding, dont be afraid to branch out to other ways to achieve better and quicker syntax
Answer by SirCrazyNugget · Mar 11, 2014 at 11:35 AM
When using one key to toggle multiple objects I store the toggled variables with a new one for activeObject, you might find it easier to do something similar, bear this in mind if Tab will later close a quest journal, character stats, etc.
Looking at what you have at the moment for toggling the Inventory you're using GetKeyUp but GetKeyDown for the crafting menu, is this right too?
This comparison should be shortened:
if(inventoryTabOn == false){
inventoryTabOn = true;
}else if(inventoryTabOn == true){
inventoryTabOn = false;
}
as you know the inventoryTabOn must be false from the previous comparison statement:
if(inventoryTabOn == false){
inventoryTabOn = true;
}else{
inventoryTabOn = false;
}
Or even shorter:
inventoryTabOn = !inventoryTabOn;
Your final comparison in the crafting menu checks every frame if it's true and sets the inventorySystem.inventoryOn to false, this should ideally be nested within all those if statements too.
Ideally, what you're trying to achieve you should be able to do without all the AND comparisons too, once you've done an initial "if" further "ifs" should be placed within those when relevant (nested or branching). With these four statements you can check everything that's required though you may need to and act on it as necessary if(Input.GetKeyDown(KeyCode.Tab)){
//if crafting is on
if(craftingOn){
//if crafting is on AND crafting tab is on
if(craftingTabOn){
//if crafting is on BUT crafting tab is off
}else{
}
//if crafting off
}else{
//if crafting is off BUT crafting tab is on
if(craftingTabOn){
//if crafting is off AND crafting tab is off
}else{
}
}
}
The explanation of what you're trying to achieve doesn't take into account all possibilities, for the future, having a picture to help know what a tab/menu is would be useful too, some can be read within the GUI setup but knowing if the Menu is located in the Tab or visa versa and what you're referring to would make it easier to visualize.
"I have these two scripts that when opened shows an inventory with two tabs." Makes perfect sense.
"If you click on the crafting menu tab then you will go to the crafting menu and it disables the inventory and same ,but the opposite way around if you then click on the inventory tab." Clicking the Crafting Tab will hide the Crafting Tab and show the Crafting Menu. The same is true for the Inventory Tab. If the Crafting Menu is visible the Inventory Tab is visible, likewise, if the Inventory Menu is visible the Crafting Tab is visible. Is this right?
"You use tab to open and close the inventory and when you click on tab the second time what should happen is all the menus and tabs should close" "Clicking on tab" is the part that's really confused me, do you mean pressing the tab key, or clicking on a tab that's already there? From what I've understood so far the Tab to open its associated Menu is hidden so you're really only interested in toggling the Inventory Tab? If that's true pressing tab the second time would require the first time to then show them (if they're not already)? If pressing tab when the Crafting Menu is open should it then show the Inventory Menu or hide all menus? Assuming the latter otherwise Tab will only ever toggle between the two menus would hopefully produce something like this:
INVENTORY:
//only use two public static variables
public static bool anyMenuOn;
public static bool inventoryOn;
//
void Update(){
if(Input.GetKeyDown (KeyCode.Tab)){
//if any of the menus are on
if(anyMenuOn){
//if inventory is on
if(inventoryOn){
//turn off all menus
anyMenuOn = false;
//if inventory isn't on
}else{
//show inventory menu
inventoryOn = true;
//turn off crafting menu (and any future menus)
Crafting.craftingOn = false;
}
//if no menus are on
}else{
//know we're in the menus
anyMenuOn = true;
//show inventory menu
inventoryOn = true;
}
}
}
void OnGUI(){
//INVENTORY
//INVENTORY QUICK SELECT BAR
//SLOT 1
GUI.Button (new Rect(489,516,78,78),inventoryDictionary[0], inventorySlotStyle);
//etc...
//TAB ON
if (anyMenuOn){
if (inventoryOn == false)
{
if(GUI.Button(new Rect(194,-25,200,200),"Inventory", inventoryTabStyle))
{
inventoryOn = true;
Crafting.craftingOn = false;
}
}
//INVENTORY ON
if (inventoryOn)
{
//INVENTORY WINDOW
GUI.BeginGroup (new Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),inventoryWindow);
//etc
GUI.EndGroup ();
}
}
}
CRAFTING:
//only use one public static variable
public static bool craftingOn;
//nothing is required in Update as we're assuming we toggle back to inventory (which is handled there)
void OnGUI(){
if(Inventory.anyMenuOn){
if (craftingOn == false)
{
if (GUI.Button (new Rect (355, -25, 200, 200), "Crafting", craftingTabStyle)){
craftingOn = true;
Inventory.inventoryOn = false;
//and all future menus to false;
}
}
if (craftingOn == true)
{
GUI.BeginGroup(new Rect(craftingWindowPosition.x,craftingWindowPosition.y,craftingWindowSize.x,craftingWindowSize.y),craftingWindow);
GUI.Label(new Rect(20, 20, 20, 20), "Crafting");
GUI.EndGroup();
}
}
}
Also, take a look at FOR loops, they'll really help tidy up your code and make it easy to read and alter
3 quick examples: where you have 34 lines of code for defining each string in the dictionary (imagine there was a thousand or more)
inventoryDictionary = new Dictionary<int, string>();
for(int i = 0; i < 34; i++){
inventoryDictionary.Add(i, string.Empty);
}
achieves the same results
Where you're drawing the GUI.Buttons for the Quick Select Bar, soon you'll need to change all those to if statements individually, it would be a lot easier to manage in a for loop again:
for(int i = 0; i < 6; i++){
if(GUI.Button (new Rect(i*88 + 491, 516, 78, 78), inventoryDictionary[i])){
//button clicked
}
}
also you'll notice you can handle each button in on codeblock now rather than 6 individual ones
Lastly, a for loop in a for loop... A little more complicated so we'll split it into 3 parts... Your boxes are numbered firstly by column so:
for(int y = 0; y < 6; y++){
if(GUI.Button (new Rect(y * 88 + 291, 64, 78, 78), inventoryDictionary[0])){ //[0] to be changed!
//button clicked
}
}
Then the rows are:
for(int x = 0; x < 4; x++){
if(GUI.Button (new Rect(291, x * 88 + 64, 78, 78), inventoryDictionary[0])){ //[0] to be changed!
//button clicked
}
}
Combining the two you will then have
for(int x = 0; x < 4; x++){
for(int y = 0; y < 6; y++){
if(GUI.Button (new Rect(y * 88 + 291, x * 88 + 64, 78, 78), inventoryDictionary[0])){ //[0] to be changed!
//button clicked
}
}
Now calculating the actual buttons ID (i), as we started with y, we multiply x by the maximum number of y, then add x, now finally adds its original offset (in this case 6).
for(int x = 0; x < 4; x++){
for(int y = 0; y < 6; y++){
int i = x * 6 + y + 6;
if(GUI.Button (new Rect(y * 88 + 291, x * 88 + 64, 78, 78), inventoryDictionary[i])){ //[0] to be changed!
//button clicked
Debug.Log(i);
}
}
}
Hopefully, this will give you a little more insight into for loops and how you can easily manage your code.
Answer by NutellaDaddy · Mar 11, 2014 at 09:24 PM
This is what the menus look like somewhat. You click the tab button to open them up and then you click the little gui button tabs to switch between the crafting button and the inventory.
Did you test any of the code at all? You've got two replies but the only response you've given is a somewhat late image.
If it's simply a form with a Toolbar I'm not sure why you're not just creating that.
I NEVER EVEN NEW THAT GUI ELE$$anonymous$$ENT EXISTED. THAN$$anonymous$$ YOU!
I'm going to one up your thing though because it helped me so much on making my code better. Thanks!
How do I define and use a toolbar I cant find anything really useful on it?
Your answer
Follow this Question
Related Questions
GUI.Button not showing up 1 Answer
GUI box open on MouseButtonDown 1 Answer
Detecting GUI.changed using DrawDefaultInspector 3 Answers
GUI click through? 3 Answers
Disable script on 2. object by boolean from 1. object. 0 Answers