Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Orloffyeah · Jul 29, 2013 at 08:25 PM · guilabelboxappear

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;
             }
         }
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Jul 29, 2013 at 08:28 PM 1
Share

Where is buyClicked set to true?

avatar image fafase · Jul 29, 2013 at 08:34 PM 0
Share

$$anonymous$$ore about what happens with coins and helmValue?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Orloffyeah · Jul 29, 2013 at 09:32 PM 0
Share

Thanks, you are awesome! I didn't knew that caracteristic of GUI.

avatar image
1

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!! ;)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Orloffyeah · Jul 29, 2013 at 09:40 PM 0
Share

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

avatar image Apexity · Jul 29, 2013 at 09:43 PM 0
Share

Yea, sorry. It's because I started this about 50 $$anonymous$$s ago and when I started there wasn't an answer XD

avatar image alonsoGarrote · Sep 16, 2013 at 06:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges