Search Unity

Closing GUI Help script

Discussion in 'Scripting' started by CranWorks, Jan 26, 2015.

  1. CranWorks

    CranWorks

    Joined:
    Jan 24, 2015
    Posts:
    3
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class HungerAndThirst : MonoBehaviour {
    6.  
    7.     //Needs
    8.  
    9.     //Health;
    10.     public int maxHealth = 100; //Health Level
    11.     public int curHealth = 100; //Current Health Level
    12.     public Texture2D max100Health; //FullHealth
    13.     public Texture2D max75Health; //75% Health
    14.     public Texture2D max50Health; //50% Health
    15.     public Texture2D max25Health; //25% Health
    16.     public Texture2D max10Health; //10% Health
    17.  
    18.     //Hunger;
    19.     public int maxHunger = 100; // Hunger Level
    20.     public int curHunger = 100; //Current Hunger Level
    21.     public Texture2D max100Hunger; //Full Hunger Texture
    22.     public Texture2D max75Hunger; //75% Hunger
    23.     public Texture2D max50Hunger; //50% Hunger
    24.     public Texture2D max25Hunger; //25% Hunger
    25.     public Texture2D max10Hunger; //10% Hunger
    26.  
    27.     //Thirst;
    28.     public int maxThirst = 100; //Thirst Level
    29.     public int curThirst = 100; //Current Thirst Level
    30.     public Texture2D max100Thirst; //Full Thirst Texture
    31.     public Texture2D max75Thirst; //75% Thirst
    32.     public Texture2D max50Thirst; //50% Thirst
    33.     public Texture2D max25Thirst; //25% Thirst
    34.     public Texture2D max10Thirst; //10% Thirst
    35.  
    36.     //Energy;
    37.    
    38.     public int maxEnergy = 100; //Max Energy
    39.     public int curEnergy = 100; //Current Energy
    40.     public Texture2D max100Energy; //Full energy texture
    41.     public Texture2D max75Energy; // 75% Energy Texture
    42.     public Texture2D max50Energy; // 50% Energy Texture
    43.     public Texture2D max25Energy; // 25% Energy Texture
    44.     public Texture2D max10Energy; // 10% Energy Texture
    45.  
    46.     //Statistics
    47.  
    48.     int curZombieKills = 0; //Current Zombie kills
    49.     int curNpcKills = 0; //Current NPC Kills
    50.     int timePlayed = 0; // Time Played In Days/Minutes/Hours
    51.     int totalMoneyEarned = 0;//Total Money Earned
    52.     int itemsPickedUp = 0; // Total Items Picked Up
    53.     int itemsSold = 0; //Total Items Sold
    54.     int milesWalked = 0; //Total Miles Walked
    55.     int carsRepaired = 0; //Total Cars Repaired
    56.     int totalDeaths = 0; //Total Deaths
    57.  
    58.     //Bar Lengths
    59.  
    60.     float healthBarLength; //Health Bar Length
    61.     float hungerBarLength; //Hunger Bar Length
    62.     float thirstBarLength; //Thirst Bar Length
    63.     float energyBarLength; //Energy Bar Length
    64.  
    65.     bool showGUI; //Bool for inventory
    66.  
    67.     void Start () { //Loads On Start
    68.         healthBarLength = Screen.width/2; //Health Bar Length
    69.         hungerBarLength = Screen.width/2; // Hunger Bar Length
    70.         thirstBarLength = Screen.width/2; // Thirst Bar Length
    71.         energyBarLength = Screen.width/2; // Energy Bar Length
    72.         }
    73.  
    74.     void Update() { //Updates every frame
    75.                 if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I"
    76.                         Debug.Log ("Opening Inventory"); //Debug
    77.                         showGUI = true; //Open GUI
    78.                 }
    79.         }
    80.  
    81.     void OnGUI() {
    82.         if (showGUI)
    83.         {
    84.             GUI.Label (new Rect (10, 10, healthBarLength, 90), "Health"+ curHealth); //GUI For Health
    85.             GUI.Label (new Rect (10, 50, hungerBarLength, 90), "Hunger:" + curHunger); //GUI For Hunger
    86.             GUI.Label (new Rect (10, 90, thirstBarLength, 90), "Thirst:" + curThirst); //GUI For Thirst
    87.             GUI.Label (new Rect (10, 130, energyBarLength, 90), "Energy:" + curEnergy);  //GUI For Energy
    88.                 {
    89.                      if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I:
    90.                         Debug.Log ("Closing Inventory"); //Debug Logger
    91.                         showGUI = false; //Closes GUI
    92.                 }
    93.             }
    94.         }
    95.     }
    96. }
     
  2. CranWorks

    CranWorks

    Joined:
    Jan 24, 2015
    Posts:
    3
    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
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Remove :

    1. {
    2. if (Input.GetKeyDown (KeyCode.I)) { //If player presses "I:
    3. Debug.Log ("Closing Inventory"); //Debug Logger
    4. showGUI = false; //Closes GUI
    5. }
    Change

    1. bool showGUI; //Bool for inventory
    to
    1. bool showGUI = false; //Bool for inventory
    Change
    1. Debug.Log ("Opening Inventory"); //Debug
    2. showGUI = true; //Open GUI
    to
    1. Debug.Log ("Opening/closing Inventory"); //Debug
    2. 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 :)
     
    CranWorks likes this.
  4. CranWorks

    CranWorks

    Joined:
    Jan 24, 2015
    Posts:
    3
    Works perfectly! Thanks alot, i will keep showGUI = !showGUI in mind for later scripts too, thanks alot.
     
    ZO5KmUG6R likes this.
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    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

     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You considered using UI instead of GUI?