OnGUI to call functions... How?

Hello,

first of all, I know that OnGUI is called multiple times per frames and as such it is not advised to call functions from it and to only use it to render GUI elements, as typically a called function will be called twice.

However, I wonder how to make relatively simple things like inventory controls work?

In my case, my inventory screen shows the stored items as DrawTextures, with their distance from the screen top controlled by a variable. The user should be able to click a GUI button to make the items move up and down so he can sort of scroll through his backpack.

The way I have scripted it now makes the items go up twice for each button press, which is to be expected but I wonder, in a general way, how such game mechanics would be designed? I’m not asking for specific code bits, just a few pointers on how to achieve this.

For clarification I include some code.

function OnGUI
{
     GUI.DrawTexture (Rect (SlotOffsetLeft,SlotOffsetTop, SlotWidth, SlotHeight), InventoryIcon);


     ...GUI button etc....
     if(Input.GetMouseButtonDown(0))
     {
          if(CallInventoryUp == false)
          {
	           CallInventoryUp = true;
          }
     }
}


function Update () 
{
	if(CallInventoryUp == true)
	{
		InventoryUp();
	}
}

function InventoryUp()
{
        SlotOffsetTop = SlotOffsetHeight + SlotMoveDistance;
        CallInventoryUp = false;
}

Note you should use events inside OnGUI() to get your mouse down. You will not get multiple MouseDown events inside OnGUI() in a singe frame. Code fragment:

var e = Event.current;
if (e.type == EventType.MouseDown) {
    CallInventoryUp = true;
}

Thanks to Bunny83 I found the solution which is indeed using Events, it even eliminates the need for seperate booleans and the Update function.

if(Event.current.type == EventType.MouseDown && Event.current.button == 0)
{
     GetComponent(Inventory).InventoryUp();
}

The function is called directly and everything else stays the same (apart from deleting the boolean).