- Home /
Why GUI Doesn't appears?
Hi, the problem is that if have a script to buy items in the game, but when I try to make a GUI Box or label, it doesn't appear, why does this happens? It only happens with one GUI the rest is working fine, it is marked in the script with the commented line.
OnGUI Buy Script:
void OnGUI()
{
if(buyClicked == true)
{
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.Label(new Rect(Screen.width/2 - 115, Screen.height/2 - 45, 250, 125), "Do You Want To Buy It?", runnerText);
if (GUI.Button (new Rect (Screen.width/2 - 100,Screen.height/2,100,60), "Yes", runnerButton))
{
coins = PlayerPrefs.GetInt("Total_Coins");
//This if statement is the GUI that doesn't works.
if(coins < helmValue)
{
GUI.Label(new Rect(Screen.width/2 - 115, Screen.height/2 - 45, 375, 187), "You Don't Have Enough Money", runnerText);
}
else if(coins >= helmValue)
{
PlayerPrefs.SetInt("Total_Coins", coins - helmValue);
PlayerPrefs.SetInt(helmOK, 1);
renderer.enabled = false;
buyClicked = false;
}
}
if (GUI.Button (new Rect (Screen.width/2 + 20,Screen.height/2,100,60), "No", runnerButton))
{
buyClicked = false;
}
}
}
$$anonymous$$ore about what happens with coins and helmValue?
Answer by Jamora · Jul 29, 2013 at 08:53 PM
The GUILabel is not shown, because it is inside an if for a button. Those are only true for one frame when they are pressed. So your GUI.Label does work, it is just shown for one frame.
You need to move it out from under the if for the button, and display it some other way. I would imagine the best way to do is to display a pop-up window that tells the player he doesn't have enough money.
This code shows you how:
bool displayPopup = false;
string popupMessage = "";
Rect windowRect;
void OnGUI ()
{
if(displayPopup){
windowRect = GUI.Window(0,windowRect,ShowPopup,"");
}else if (buyClicked == true) {
GUI.Box (new Rect (0, 0, Screen.width, Screen.height), "");
GUI.Box (new Rect (0, 0, Screen.width, Screen.height), "");
GUI.Label (new Rect (Screen.width / 2 - 115, Screen.height / 2 - 45, 250, 125), "Do You Want To Buy It?", runnerText);
if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2, 100, 60), "Yes", runnerButton)) {
//This if statement is the GUI that doesn't works.
if (coins < helmValue)
{
displayPopup = true;
popupMessage = "You Don't Have Enough Money";
windowRect = new Rect (Screen.width / 2 - 115, Screen.height / 2 - 45, 375, 187);
}else if(coins >= helmValue)
{
PlayerPrefs.SetInt("Total_Coins", coins - helmValue);
PlayerPrefs.SetInt(helmOK, 1);
renderer.enabled = false;
buyClicked = false;
}
}
if (GUI.Button (new Rect (Screen.width / 2 + 20, Screen.height / 2, 100, 60), "No", runnerButton)) {
buyClicked = false;
}
}
}
void ShowPopup(int id){
GUILayout.Label("\n\n");
GUILayout.Label ("You Don't Have Enough Money", runnerText);
if(GUILayout.Button("OK"))
displayPopup = false;
}
Thanks, you are awesome! I didn't knew that caracteristic of GUI.
Answer by Apexity · Jul 29, 2013 at 09:36 PM
You are displaying the message only for the frame that the button is clicked. This can be fixed by adding a couple variables to help display a message for a short time. Also, you should show us the whole script otherwise we can't really help you. I can also say a few things of advice. 1: You should clean up the script a bit and make it easier to look at. 2: Make sure that helmValue and coins are actually working. Maybe by adding a Debug.Log line in the code to show if the values are actually less than eachother or if there even set just in case this doesn't fix the problem! 3: You don't have to put if(var == true), you can just put the var's name for the equivalent result 4: Set the value of coins when the GUI is supposed to pop up. Basically, wherever the buyClicked var is set to true. The following is my own go at the script. I'm not really fluent in C# though, haha, so sorry if there are a few mistakes! (I'm a Javascripter ^-^)
GUIStyle runnerButton;
GUIStyle runnerText;
bool buyClicked = false;
bool displayMessage = false;
string message = "";
float timer = 0;
int coins;
int helmValue = 100;
void OnGUI ()
{
if(buyClicked)
{
if(displayMessage)
{
GUI.Label(new Rect(Screen.width/2 - 115, Screen.height/2 - 45, 375, 187), "" + message, runnerText);
//The following line adds the time between each frame, each frame
timer += Time.deltaTime;
if(timer >= 3)
{
//Once the time is greater than 3, we basically disable the message
timer = 0;
message = "";
displayMessage = false;
}
}
//No need to draw the same box twice.
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.Label(new Rect(Screen.width/2 - 115, Screen.height/2 - 45, 250, 125), "Do You Want To Buy It?", runnerText);
if (GUI.Button (new Rect (Screen.width/2 - 100,Screen.height/2,100,60), "Yes", runnerButton))
{
//Here, we can log the current value of coins and helmValue to see
//if the Player's coins is actually less than the Helmet's Cost
Debug.Log("Coins: " + coins + " | Helmet Cost: " + helmValue);
if(coins < helmValue)
{
message = "You Don't Have Enough Money";
displayMessage = true;
}
//Don't need to put 'else if' if there is only one other possibility
else
{
PlayerPrefs.SetInt("Total_Coins", coins - helmValue);
PlayerPrefs.SetInt(helmOK, 1);
renderer.enabled = false;
buyClicked = false;
}
}
if (GUI.Button (new Rect (Screen.width/2 + 20,Screen.height/2,100,60), "No", runnerButton))
{
buyClicked = false;
}
}
}
void OnTriggerEnter (hit : Collider)
{
//Here I set a quick scenario for if the player comes in contact with a helmet at the store.
//Take note that I set the coins value when buyClicked is being set to true
//instead of when the button's clicked.
if(hit.tag == "Player")
{
buyClicked = true;
coins = PlayerPrefs.GetInt("Total_Coins");
}
}
Alright, I really hope I helped you mate. Feel free to post anymore problems!! ;)
Thanks for the reply, it is very complete, but I already got the solution from Jamora, thanks anyway, you both came up with the same solution :D
Yea, sorry. It's because I started this about 50 $$anonymous$$s ago and when I started there wasn't an answer XD
Hi there, I have a problem that I think might be GUI related stuff..., $$anonymous$$y GUIlayout doesnt show up when I touch a button...Can anybody help on this please?: link text
Your answer
Follow this Question
Related Questions
OnGUI Labels affect different boxes? 1 Answer
Trouble Positioning a GUILayout.Label, or as a button? 0 Answers
when button pressed, textfield appears 3 Answers
GUI box not appearing when button is clicked 1 Answer
Make GUI box appear and disappear 2 Answers