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 RuneyWolf · Jan 23, 2014 at 10:08 PM · c#guifunctions

Making a GUI panel appear on boolean true

EDIT: Code updated

Hello everyone,

I'm working with some GUI code that's written in OnGUI(){}. I have several functions and booleans set up to display these GUI windows, but it isn't working.

Basically when a user presses the "Store Dust From EVA" button it should pop up a window telling them how much dust is stored. It's based on a boolean. However it isn;t working and I have no idea why. Anyone care to look?

 using UnityEngine;
 using System.Collections;
 
 public class Factory : MonoBehaviour {
 
 
     //Private Bools
     private bool showGUI;
     private bool clickedRock;
     private bool clickedDust;
     private bool clickedStore;
     private bool notEnoughDust;
 
     //Static Vars
     public int rocks = PlayerClass.rocks;
     public int iron = PlayerClass.iron;
     public int dust = PlayerClass.iron_dust;
 
     //Inhouse Vars
     public int storedDust = 0;
 
 
     // Use this for initialization
     void Start () {
         showGUI = false;
         notEnoughDust = false;
     }
     
     // Update is called once per frame
     void Update () {
         RefineRocks();
         RefineDust();
         StoreDust();
         Debug.Log("Not Enough Dust Is: " + notEnoughDust);
     }
 
     void OnTriggerEnter(){
             showGUI = true;
     }
 
     void OnTriggerExit(){
         showGUI = false;
     }
 
     void RefineRocks(){
         if(clickedRock == true){
             for(int i = 0; i < rocks; i++){
                 rocks--;
                 iron++;
                 Debug.Log("Rocks: " + rocks + " Iron: " + iron);
             }
         }
     }
 
     void RefineDust(){
         if(clickedDust == true){
                     dust-= 10;
                     iron++;
                     Debug.Log("Rocks: " + rocks + " Iron: " + iron);
             notEnoughDust = false;
                 }else if(clickedDust == true && dust < 10){
             notEnoughDust = true;
             }
         }
 
     void StoreDust(){
         if(clickedStore == true){
             storedDust += dust;
             dust = 0;
         }
     }
 
     void OnGUI(){
         if(showGUI == true){
             //This is the Background Box
             GUILayout.BeginArea(new Rect(500, 480, 600, 600), " ");
 
             GUILayout.Box("Factory", GUILayout.Width(600), GUILayout.Height(600));
 
             GUILayout.EndArea();
 
             //This is the buttons
             GUILayout.BeginArea(new Rect(500, 500, 600, 600), " ");
 
             GUILayout.BeginVertical();
 
             if(GUILayout.Button("Refine Snow", GUILayout.Width(200))){
                 //Stuff
             }
 
 
             GUILayout.Space(10);
 
 
             if(GUILayout.Button("Refine Rocks", GUILayout.Width(200))){
                 if(rocks >= 1){
                     clickedRock = true;
                 }
             }
 
 
             GUILayout.Space(10);
 
 
             if(GUILayout.Button("Refine Dust", GUILayout.Width(200))){
                 if(dust >= 10){
                     clickedDust = true;
                 }
             }
 
 
             GUILayout.Space(20);
 
 
             if(GUILayout.Button("Store Dust from EVA", GUILayout.Width(200))){
                 clickedStore = true;
             }
 
             GUILayout.EndVertical();
 
             GUILayout.EndArea();
 
             if(notEnoughDust == true){
                 GUI.Box(new Rect(700, 500, 200, 100), "You need 10 dust!");
             }
 
             if(storedDust > 0){
                 Debug.Log ("This works");
                 GUI.Box(new Rect(700, 500, 200, 100), "Stored Dust:");
                 GUI.Label(new Rect(700, 510, 200, 200), "Dust: " + storedDust);
             }
         }
     }
 }
 

Thanks for the help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 24, 2014 at 10:44 AM

This looks wrong:

  for(int i = 0; i < dust; i++){
   dust--;
   storedDust++;
  }

This will never run [dust] times, because you increase i and decrease dust. In the best case this will run dust/2 times.

It looks like what you're trying to do is:

 storedDust += dust;
 dust = 0;

There's no need for a loop. And should you need a loop there later and you want to move the dust one by one, you should use something like

 while(dust>0) {...}

Also, I don't see anywhere in your code the dust variable being increased, so you can never reach the code we're talking about.

And what do you need dustIsStored for? That looks like something that could make problems later. Why not just check for replace every

 dustIsStored==true

with

 storedDust > 0

and get rid of the DisplayDust method altogeher?

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 RuneyWolf · Jan 24, 2014 at 02:41 PM 0
Share

Thanks for the help. I did what you said and the GUI stuff is still not popping up anywhere. Does anyone know why?

avatar image HappyMoo · Jan 24, 2014 at 04:37 PM 0
Share

update the code above with what you have now. Where do you get dust now

avatar image RuneyWolf · Jan 24, 2014 at 09:08 PM 0
Share

Code has been updated.

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to make my OnTriggerEnter() and Exit work 1 Answer

Generating tooltips for multiple buttons. 1 Answer

Handling Undo/REdo in EditorWindow when editing DB record. 0 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