Game goes down from 110 to 25 fps by using OnGUI()


I have 1500 existing objects. Every object has a script attached. Every script also has an OnGUI() section. I am using it to draw text labels above each objects. I already did some optimization but for some strange reason, its always slow as hell. Just for trying out I did a return; immediately after entering the sub:

void OnGUI() {

But still, I have only 25 fps. Only commenting the while OnGui block out and it gets up to 110 fps again...

Is this a bug in Unity or is the only way implementing labels over objects by iterating every object in the main class, checking if it is visible, and then drawing the label? This would make the whole thing very complex and... uncool....

Is there a way to really disable the OnGUI calls as long as the object is not visible?

It's not a bug (having 1500 OnGUI functions being called every frame is a lot of overhead regardless of whether OnGUI does anything or not), and checking whether every object is visible and so on isn't the only way. Do this:

void OnBecameVisible () {
    enabled = true;

void OnGUI () {
    // whatever

void OnBecameInvisible () {
    enabled = false;

This is just a stray thought, so bear with me. :)

Perhaps you could create an empty GameObject called GUIHandler or something, equip it with a script with an OnGui-method, then expose the array of 1500 objects to it, then handle the addition and manipulation of labels to all 1500 objects from that single OnGui-method, rather than one OnGui for each object?