How do I change GUI elements from outside of the OnGUI function?

I have an array with 200 elements that have to be placed on the GUI layer once. After that, sometimes I will have to delete one or two of these.

Right now I'm using

`for(var i=0;i<itemArray.length;i++){`

To loop through it all, but it then does this EVERY onGUI call... making it extremely slow. Is there a way to place the elements once and when a change is made in another function only update this single element?

This is the script I'm using inside OnGUI():

for(var i=0;i<AgendaArray.length;i++){
     var taskBegin = AgendaArray*[1];*
 _var taskEnd = AgendaArray*[2];*_
 <em>_var taskName = AgendaArray*[0];*_</em>
 <em><em>_var taskStatus = AgendaArray*[5];*_</em></em>
 <em><em>_*var thisStyle : GUIStyle;*_</em></em> 
 <em><em><em>_if(timer >= taskBegin && timer <= taskEnd && taskStatus != 2){AgendaArray*[5] = 1;}*_</em></em></em>
 <em><em><em><em>_else if(timer>taskEnd && taskStatus != 2){AgendaArray*[5] = 3;}*_</em></em></em></em>
 <em><em><em><em>_*if(taskStatus == 1){thisStyle = activeStyle;}*_</em></em></em></em>
 <em><em><em><em>_*else if(taskStatus == 2){thisStyle = passedStyle;}*_</em></em></em></em>
 <em><em><em><em>_*else if(taskStatus == 3){thisStyle = failedStyle;}*_</em></em></em></em>
 <em><em><em><em>_*else{thisStyle = inactiveStyle;}*_</em></em></em></em>
 <em><em><em><em><em>_var taskPosition = Screen.width*(taskBegin-beginOfTheDay)/lengthOfDay;_</em></em></em></em></em>
 <em><em><em><em><em>_var taskWidth = Screen.width*((taskEnd-taskBegin)/lengthOfDay);_</em></em></em></em></em>
 <em><em><em><em>_*GUI.Box(Rect(taskPosition,Screen.height-35,taskWidth,30),taskName,thisStyle);*_</em></em></em></em>
 <em><em><em><em>_*}*_</em></em></em></em>
<em><em><em><em>_*```*_</em></em></em></em>

In most cases, you just have to draw your elements every frame. If this is giving you realworld performance problems (measure, not guess!), you could create a texture on the fly, render them into that, and then only render the combined texture at runtime.

The Unity GUI system is an "immediate mode" rendering system, which means there's no GUI objects that are created and maintain their own state (although you could create such a system if you wanted to). So each time OnGUI is called you will need to redraw all of your boxes. Now, as others have suggested, you may be able to solve your performance problems in other ways. The most obvious would seem to be moving your logic for determining a style and box width into some sort of Agenda object that can maintain the needed state so you're only recalculating when something changes.

OnGUI was not designed to be something that you'd actually ship with a product. It's only there so that you can create UI quickly for debugging purposes, or just for placeholders. For a real, shippable UI, you need to roll your own, probably using GuiTexture and GuiText.