Im basically writing an inventory script with loads of different sections (crafting, statistics, save/load,needs) etc, i need help with the closing of the inventory. Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class HungerAndThirst : MonoBehaviour { //Needs //Health; public int maxHealth = 100; //Health Level public int curHealth = 100; //Current Health Level public Texture2D max100Health; //FullHealth public Texture2D max75Health; //75% Health public Texture2D max50Health; //50% Health public Texture2D max25Health; //25% Health public Texture2D max10Health; //10% Health //Hunger; public int maxHunger = 100; // Hunger Level public int curHunger = 100; //Current Hunger Level public Texture2D max100Hunger; //Full Hunger Texture public Texture2D max75Hunger; //75% Hunger public Texture2D max50Hunger; //50% Hunger public Texture2D max25Hunger; //25% Hunger public Texture2D max10Hunger; //10% Hunger //Thirst; public int maxThirst = 100; //Thirst Level public int curThirst = 100; //Current Thirst Level public Texture2D max100Thirst; //Full Thirst Texture public Texture2D max75Thirst; //75% Thirst public Texture2D max50Thirst; //50% Thirst public Texture2D max25Thirst; //25% Thirst public Texture2D max10Thirst; //10% Thirst //Energy; public int maxEnergy = 100; //Max Energy public int curEnergy = 100; //Current Energy public Texture2D max100Energy; //Full energy texture public Texture2D max75Energy; // 75% Energy Texture public Texture2D max50Energy; // 50% Energy Texture public Texture2D max25Energy; // 25% Energy Texture public Texture2D max10Energy; // 10% Energy Texture //Statistics int curZombieKills = 0; //Current Zombie kills int curNpcKills = 0; //Current NPC Kills int timePlayed = 0; // Time Played In Days/Minutes/Hours int totalMoneyEarned = 0;//Total Money Earned int itemsPickedUp = 0; // Total Items Picked Up int itemsSold = 0; //Total Items Sold int milesWalked = 0; //Total Miles Walked int carsRepaired = 0; //Total Cars Repaired int totalDeaths = 0; //Total Deaths //Bar Lengths float healthBarLength; //Health Bar Length float hungerBarLength; //Hunger Bar Length float thirstBarLength; //Thirst Bar Length float energyBarLength; //Energy Bar Length bool showGUI; //Bool for inventory void Start () { //Loads On Start healthBarLength = Screen.width/2; //Health Bar Length hungerBarLength = Screen.width/2; // Hunger Bar Length thirstBarLength = Screen.width/2; // Thirst Bar Length energyBarLength = Screen.width/2; // Energy Bar Length } void Update() { //Updates every frame if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I" Debug.Log ("Opening Inventory"); //Debug showGUI = true; //Open GUI } } void OnGUI() { if (showGUI) { GUI.Label (new Rect (10, 10, healthBarLength, 90), "Health"+ curHealth); //GUI For Health GUI.Label (new Rect (10, 50, hungerBarLength, 90), "Hunger:" + curHunger); //GUI For Hunger GUI.Label (new Rect (10, 90, thirstBarLength, 90), "Thirst:" + curThirst); //GUI For Thirst GUI.Label (new Rect (10, 130, energyBarLength, 90), "Energy:" + curEnergy); //GUI For Energy { if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I: Debug.Log ("Closing Inventory"); //Debug Logger showGUI = false; //Closes GUI } } } } }
Oh and btw this isnt finished yet ;p Hence the reason why its so small. This is also my first actual script im gonna be releasing so dont judge if its messy ;p
Remove : { if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I: Debug.Log ("Closing Inventory"); //Debug Logger showGUI = false; //Closes GUI } Change bool showGUI; //Bool for inventory to bool showGUI = false; //Bool for inventory Change Debug.Log ("Opening Inventory"); //Debug showGUI = true; //Open GUI to Debug.Log ("Opening/closing Inventory"); //Debug showGUI = !showGUI; //Open or close GUI Explanation: showGUI = !showGUI means it will make it false if true or true or false, a handy little operator
Works perfectly! Thanks alot, i will keep showGUI = !showGUI in mind for later scripts too, thanks alot.
Another suggestion : For the bars you should just stretch a GUITexture rather than use 4 each. It'll help you in the long run I've packaged up a little thing with 2 textures and a demo script to show what I mean https://www.sendspace.com/file/964de8