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.
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Runtime.Serialization.Formatters.Binary;
public class GameShopGUI : MonoBehaviour
{
public MoneySaveLoad saveLoadSystem;
public MoneyHolder moneyHold;
public GUIText gameShopGUIText;
void Start ()
{
saveLoadSystem.Load();
}
void Update ()
{
gameShopGUIText.text = "The Dumb Shop";
gameShopGUIText.fontSize = 100;
gameShopGUIText.pixelOffset = new Vector2(200, 285);
}
void OnGUI ()
{
if (GUI.Button(new Rect(150, 300, 150, 75), "Buy 10 Extra Dumb Bullets" + "\n" + "(10 Dumb Coins)"))
{
if (moneyHold.amountOfDumbCoins >= 15)
{
saveLoadSystem.amountOfExtraDumbBullets += 10;
moneyHold.amountOfDumbCoins -= 10;
}
if (moneyHold.amountOfDumbCoins <= 14)
{
moneyHold.dumbCoinsLow = true;
if (moneyHold.dumbCoinsLow == true)
{
GUI.Box(new Rect(10, 10, 50, 50), "test");
}
}
}
}
}
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!
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.
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:**[/u] 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.