Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[C#]OnGUI GUI.Button If Statement not working. (Please help)

Discussion in 'Scripting' started by KyleStank, May 3, 2014.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I am trying to do some conditions in the void OnGUI() method and it is not working. When I define some if statements under GUI.Button, it does not work. When I click the button, nothing happens. The only thing that works is if I say Debug.Log("some text"); under the if statements. NOTHING else works. And by the way, GUI.RepeatedButton() will not work correctly. I tried to use it but it destroyed the GUI.Box that I am trying to create. Anyway, here is my code for the script.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6.  
    7. public class GameShopGUI : MonoBehaviour
    8. {
    9.     public MoneySaveLoad saveLoadSystem;
    10.     public MoneyHolder moneyHold;
    11.     public GUIText gameShopGUIText;
    12.     void Start ()
    13.     {
    14.         saveLoadSystem.Load();
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.         gameShopGUIText.text = "The Dumb Shop";
    20.         gameShopGUIText.fontSize = 100;
    21.         gameShopGUIText.pixelOffset = new Vector2(200, 285);
    22.     }
    23.  
    24.     void OnGUI ()
    25.     {
    26.         if (GUI.Button(new Rect(150, 300, 150, 75), "Buy 10 Extra Dumb Bullets" + "\n" + "(10 Dumb Coins)"))
    27.         {
    28.             if (moneyHold.amountOfDumbCoins >= 15)
    29.             {
    30.                 saveLoadSystem.amountOfExtraDumbBullets += 10;
    31.                 moneyHold.amountOfDumbCoins -= 10;
    32.             }
    33.             if (moneyHold.amountOfDumbCoins <= 14)
    34.             {
    35.                 moneyHold.dumbCoinsLow = true;
    36.                 if (moneyHold.dumbCoinsLow == true)
    37.                 {
    38.                     GUI.Box(new Rect(10, 10, 50, 50), "test");
    39.                 }
    40.             }
    41.         }
    42.     }
    43. }
    44.  
    Can someone please help me? This is REALLY annoying. I just want to make a shop system for my game and I cannot do it if I cannot get this to work!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    WIth your code the GUI.Box is only drawn for a very, very short time because the code will only be executed once when you pressed the button. Draw the GuiBox outside of the button part.
     
  3. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    No, this will not work because I need it to bring a pop box when you don't have enough money to purchase something. Like say it costs $10 and the player has $11, then it brings a box up that says "Thank You" and then it draws a button that says "Dismiss Message" or something similar(haven't added this yet though but planning on to) and then if the player doesn't have $10, then it brings up a box that says "You do not have enough money" and then it too draws a button that will say "Dismiss Message." If I draw it outside the button, it will always be there and I do not want that. I only want a button and box to appear if the moneyHold.dumbCoinsLow == true. Thanks for helping though because that would work but I don't need it to work in that way.

    EDIT:
    Never mind. I read that wrong and I see what you meant. At least I think I do. I did the check to see if the player had enough money and if he/she did, then it gave the power up and took away the money and made the boolean variables false. Then I did an else if statement saying it the player didn't have enough money, then made the boolean true. Then, outside the button, I said if the boolean == true, then draw a GUI.Box and a GUI.Button. And when the GUI.Button was clicked, then the boolean was made false and the box went away until the powerup button was clicked again. Thank you for helping Suddoha.
     
    Last edited: May 3, 2014