Optimise Menu

When i look in the profiler and eneble my pauseGame script the fps drops from 1000+ to 30-60 now something is obviously really wrong and i think it is the amount of variables i have and considering OnGUI is run every frame. All i am asking is for some advice on to optimise it. here is the code and it is a big one! but works very well i just want as much performance as possible.

// Static variables
static var paused : boolean = false;

var mouseOrbit;
var dynamite : Texture2D;
var pauseGUI : GUIStyle;
var resumeGUI : GUIStyle;
var optionsGUI : GUIStyle;
var weaponsGUI : GUIStyle;
var quitGUI : GUIStyle;
var tipsGUI : GUIStyle;
var pressed : boolean = false;
var backGUI : GUIStyle;
var dynamiteGUI : GUIStyle;
var resume : boolean = false;
var weapons : boolean = false;
var quit : boolean = false;
var tips : boolean = false;
var options : boolean = false;
var graphics : boolean = false;
var sound : boolean = false;
var controls : boolean = false;
var stats : boolean = false;
var electroDrillMenu : boolean = false;

// Weapons GUI Goes Here
var electroDrillBack : GUIStyle;
var electroDrillBack2 : GUIStyle;
var electroDrillUpgradeScreen : boolean = false;
var electroDrillUpgradeButton : GUIStyle;
var electroDrillBullet : GUIStyle;
var electroDrillForce : GUIStyle;
var electroDrillRate : GUIStyle;
var electroDrillDamage : GUIStyle;
var electroDrillAmmo : GUIStyle;
var backTexture : GUITexture;
var none : Texture2D;

// Options GUI Goes Here
var graphicsGUI : GUIStyle;
var soundGUI : GUIStyle;
var controlsGUI : GUIStyle;
var statsGUI : GUIStyle;

// Graphics GUI Goes here;
var fastestGUI : GUIStyle;
var fastGUI : GUIStyle;
var simpleGUI : GUIStyle;
var goodGUI : GUIStyle;
var beautifulGUI : GUIStyle;
var fantasticGUI : GUIStyle;

// for controls menu
var controlGUI : GUIStyle;

// For menu Scripts
var electroDrill : boolean = false;


function Update ()
{
    if(Input.GetKeyUp(KeyCode.Escape) || Input.GetKeyUp(KeyCode.JoystickButton7))
    {
        if(!paused)
        {
            Time.timeScale = 0;
            paused = true;
            mouseOrbit = gameObject.Find("Main Camera").GetComponent("CameraOrbitZoom").enabled = false;
            mouseOrbit = gameObject.Find("Camera").GetComponent("MouseLook").enabled = false;
        }
        else
        {
            Time.timeScale = 1;
            paused = false;
            weapons = false;
            pressed = false;
            options = false;
            controls = false;
            graphics = false;
            electroDrill = false;
            electroDrillUpgradeScreen = false;
            backTexture.enabled = false;
            mouseOrbit = gameObject.Find("Main Camera").GetComponent("CameraOrbitZoom").enabled = true;
            mouseOrbit = gameObject.Find("Camera").GetComponent("MouseLook").enabled = true;
        }
    }
    Debug.Log("Weapons is " +weapons);
    Debug.Log("electroDrill is" + electroDrill);
}


function OnGUI()
{
    if(paused == true)
    {
        if(pressed == false)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,resumeGUI))
            {
                paused = false;
                Time.timeScale = 1.0;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,optionsGUI))
            {
                options = true;
                pressed = true;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,weaponsGUI))
            {
                weapons = true;
                pressed = true;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 20,256,64),dynamite,tipsGUI))
            {
                tips = true;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 +80,256,64),dynamite,quitGUI))
            {
                paused = false;
                weapons = false;
                options = false;
                graphics = false;
                pressed = false;
                Time.timeScale = 1.0;
                Application.LoadLevel(0);
            }
        }
        
        if(weapons == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 380 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                electroDrill = true;
                //paused = false;
                weapons = false;
                Debug.Log("We preesed the electroDrill button we should now see the electro drill");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 250 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                
            }
            
            if(GUI.Button(Rect(Screen.width / 2 + 10 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +140 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +270 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            
            // this is the second row 
            if(GUI.Button(Rect(Screen.width / 2 - 380 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
            {
                GunsAmmo.electroDrillAmmo += 30;
                GameStats.parts -= 500;
                Debug.Log(GameStats.parts);
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 250 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +10 , Screen.height / 2 +10,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +140 , Screen.height / 2 +10,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +270 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 +140 ,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 +10 , Screen.height / 2 +140,128,128),dynamite,dynamiteGUI))
            {
                print("You are the best 2nd button pressed");
            }
            
            if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),dynamite,backGUI))
            {
                weapons = false;
                pressed = false;
            }
        }

        if(options == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,graphicsGUI))
            {
                graphics = true;
                options = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,controlsGUI))
            {
                controls = true;
                options = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,statsGUI))
            {
                
            }
             
            if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
            {
                options = false;
                pressed = false;
            }
        }// with options 
        
        if(graphics == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,fastestGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Fastest;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,fastGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Fast;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,simpleGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Simple;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 20,256,64),dynamite,goodGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Good;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 80,256,64),dynamite,beautifulGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Beautiful;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 140,256,64),dynamite,fantasticGUI))
            {
                QualitySettings.currentLevel = QualityLevel.Fantastic;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
            {
                graphics = false;
                options = true;
            }
        }
        
        if(controls == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 - 150,512,512),dynamite,controlGUI))
            {
                
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
            {
                controls = false;
                options = true;
            }
        }
        
        if(electroDrill == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 - 256,512,512),none,electroDrillBack))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),none,backGUI))
            {
                electroDrill = false;
                weapons = true;
            }
        }
        
        if(electroDrillUpgradeScreen == true)
        {
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +10,512,32),none,electroDrillBullet))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +60,512,32),none,electroDrillRate))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +110,512,32),none,electroDrillForce))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +160,512,32),none,electroDrillAmmo))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +210,512,32),none,electroDrillDamage))
            {
                electroDrillUpgradeScreen = true;
                electroDrill = false;
            }
            
            if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),none,backGUI))
            {
                electroDrill = true;
                backTexture.enabled = false;
                electroDrillUpgradeScreen = false;
            }
            
            backTexture.enabled = true;
            
            if(electroDrillUpgradeScreen == false)
            {
                backTexture.enabled = false;
            }
        }
    }
}

Well, I did ONLY a quick view of your code.

You have commited some of the capital sin of programming in your code

  1. Never use GameObject.Find inside of Update! Same goes for GetComponent. Cache it in a variable, and access the variable instead. .Find/.GetComponent are SLOW
  2. Similar to rule one: NEVER call Debug.Log on each Update/FixedUpdate! It’s HORRIBLY SLOW, even if it only appears in debug builds & Editor, it will have even bigger impact with Profiler enabled.
  3. Unity GUI in general is pretty slow, especially on mobile phones. Each GUI object will cause a draw call, and having more than 50 draw calls on a smartphone lowers the performance drastically. On PC it’s not that worse (1000-1500 draw calls or more are no problem with modern PCs).

You should consider caching the various numbers used in your calculation in variables. For example, you are working a lot with height/2 and width/2. You can store those in a variable to avoid re-calculating those values.

You can also use OnGUI to display debug messages, instead of using Debug.Log.

As a way to improve the organization, you are using one big GUI script to handle all your screens. Rather, you can split those screens into three different gameobject and only enable the one which is active.

Without reading through this mess i can almost for certain tell you that your Debug.Log’s will cause this fps-drop. Debug.Log() (or print()) is THE fps killer inside the editor. In builds it doesn’t seem to drop that much since the text isn’t displayed but still written to the logfile.