Why is the InventoryGUI method being called endlessly?

int myInventory = new int[40];
string user = “potato”;
Item AllItems;
int iconWidthHeight = 40; // The pixel size (height and width) of an inventory slot
int spacing = 5; // Space between slots (in x and y)
Vector2 offSet;
Texture2D emptySlot;
bool openInventoryWindow = false;
Rect inventoryWindow = new Rect (10, 10, Screen.width - 10, Screen.height - 10);
public Texture2D emptyInventorySlot;

	int testInvLength;
	
	void Start () {

		openInventoryWindow = false;

		for (int i = 0; i < myInventory.Length; i++) 
		{ 
			myInventory *= 0;* 
  •  }*
    
  • }*

  • IEnumerator pullInv(WWW w){*

  •  if (w.error == null){*
    
  •  	print(w.text);*
    
  •  	string big = w.text;*
    
  •  	string[] sValues = big.Split(',');*
    
  •  	int[] values = new int[sValues.Length];*
    
  •  	for (int i = 0; i < sValues.Length; i++){*
    

myInventory = int.Parse (sValues*); //this populates the array myInventory with the item ID’s of the items in it*
* }*
* }*
* else{*
* print (w.error);*
* }*
* yield return w;*
* }*

* void InventoryGUI(int WindowID){*
* if (GUI.Button (new Rect (5,5,10,10), “”)) // Left upper corner quit button*
* {*
* CloseInventoryWindow ();*
* }*
* int j;*
* int k;*
* Rect currentRect;*
* for (int i = 0; i < myInventory.Length; i ++) {*
* j = i / myInventory.Length;*
* k = i % myInventory.Length;*
_ currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));
* print (i);
if (myInventory == 0)
{
GUI.DrawTexture (currentRect, emptyInventorySlot);
}
else*

* {
GUI.DrawTexture (currentRect, AllItems[myInventory].texture);
}
}
}*_

* void Update () {*
* if (Input.GetKeyUp (KeyCode.I)){*
* openInventoryWindow = !openInventoryWindow;*
* }*
* }*

* void OnGUI(){*
* if (openInventoryWindow)*
* {*
* GUI.Window (1, inventoryWindow, InventoryGUI, “Inventory”);*
* }*
* }*

* void CloseInventoryWindow (){*
* {*
* openInventoryWindow = false;*
* }*
* }*
For some reason when I hit the play button and hit I the “InventoryGUI” method is called endlessly, proven by line 53. I just want the inventory screen to pop up without being called endlessly, creating tons of lag.
I think the main issue lies inside of the boolean used to open and close the variable, but I can’t pinpoint the problem. Any ideas?

Unity’s GUI system is an immediate mode GUI system. That means you have to redraw the GUI every frame. For that reason Unity calls OnGUI every frame so the GUI is redrawn. OnGUI is also called when an input event occurs such as a mouse down / up or key down / up.

The OnGUI method as well as your window function don’t “create” the window, they actually draw it and handle input events. There’s no way to only call it once.