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!

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?