How to use GUI.depth properly

I’m trying to make sure that, in the GUI content generated by the following for loop, my tooltips will always be displayed on top of the other content. Unfortunately, this doesn’t always happen because, due to the fact that each image and its corresponding tooltip are generated in the same loop, each image has a GUI depth lower than the one before it, resulting in the tooltip being covered by that image. Here’s my code:

var items = GetComponent(PlayerInventory).inventory;
for(var i = 0; i < items.Count; i++) {
	var curItem = items*;*
  •   var x = i % inventorySize;*
    
  •   if(x == 0 && i > 0) {*
    
  •   	x = 0;*
    

_ y += itemDisplaySize + itemDisplayPadding * ((i % inventorySize > ) ? 1 : i);_

  •   }*
    

_ var xPos = x * itemDisplaySize + (itemDisplayPadding * (i % inventorySize));_

  •   GUI.depth = 1;*
    
  •   GUI.Box(new Rect(xPos, y, itemDisplaySize, itemDisplaySize), GUIContent(curItem.image, curItem.name));*
    
  •   if(GUI.tooltip == curItem.name) {*
    
  •       GUI.depth = 0;*
    
  •       GUI.Label (Rect (xPos + 10,y+20,200,40), GUI.tooltip);*
    
  •   }*
    
  • }*
    You can ignore the stuff about positioning. Now, when I mouse over one of the boxes that I created, its tooltip appears as expected. However, the tooltip is covered by the next image. I assumed GUI.depth would be the solution, but it unfortunately isn’t. Any ideas?

GUI.depth only works with separate OnGUI functions, not in the same script.