Unity performance issues in my scene.

Hi guys! I am having performance issues on my iPhone 4G in my application.

I have screen shotted the stats. Works ok on my iPad 3 but really badly on my iPhone 4G. I am not sure what is causing the lag.
Ill paste my source code to see if anyone can analyse my code and identify any problems. Thanks :).

[8100-screen+shot+2013-02-20+at+22.05.03.png|8100]

Here is my enemy controller:

#pragma strict

var enemy1Prefab : GameObject;
var bombEnemy2Prefab : GameObject;
var bombermanBossPrefab : GameObject;
var eggEnemyPrefab : GameObject;
var cat : GameObject;
var bossToSpawn : boolean;
var mainCamera : GameObject;
var bombBossClip : AudioClip;
var bombUniverseClip : AudioClip;
var cylinder : GameObject;
var bombBackground : Texture;
var rainbowBackground : Texture;
var frameCountFromStart : int;

function Start () {
bossToSpawn = true;
}

function Update () {
frameCountFromStart++;

if(Time.frameCount < 30 && Time.frameCount %2 == 0){
Instantiate(enemy1Prefab, transform.position, transform.rotation);
}

//GameObject.FindGameObjectsWithTag("enemy").Length < 10 
//GAME OBJECT LAYER 10 IS ENEMIES
if(FindGameObjectsWithLayer(10).Length < 6 && Time.time > 1.0f && Time.time < 30.0f){
	var bomben : GameObject = Instantiate(enemy1Prefab, transform.position, transform.rotation) as GameObject;
	//var enemy : GameObject = Instantiate(bombermanPrefab, transform.position, transform.rotation);
} else  if(FindGameObjectsWithLayer(10).Length < 12 && Time.time > 30.0f && Time.time < 600.0f){
    //Debug.Log("GameObject.FindGameObjectsWithTag('enemy').Length >= 4");
   	var randnum : float = Random.value;
   	if(randnum < 0.5){
    Instantiate(bombEnemy2Prefab, transform.position, transform.rotation);
    } else {
    Instantiate(enemy1Prefab, transform.position, transform.rotation);
    }
}


// GAMEOBJECT LAYER 10 IS ENEMIES
var enemies : GameObject[] = FindGameObjectsWithLayer(10);



for(var i = 0; i < enemies.length; i++){
	//Debug.Log("checking enemies"  + i);
	var eneBehaviour : enemyBehaviour = enemies*.GetComponent(enemyBehaviour);*
  • //Debug.Log(“health is” + eneBehaviour.health);*
    _ if(enemies*.transform.childCount == 0){_
    _ Destroy(enemies);
    var score : int = PlayerPrefs.GetInt(“player score”);
    score += 5;
    PlayerPrefs.SetInt(“player score”, score);
    //Debug.Log("should destroy enemies");
    }
    }
    if(frameCountFromStart < 2){
    mainCamera.audio.clip = bombUniverseClip;
    mainCamera.audio.Play();
    mainCamera.audio.loop = true;
    }*_

if(frameCountFromStart >= 2000 && bossToSpawn == true){

cylinder.renderer.material.mainTexture = bombBackground;
mainCamera.audio.clip = bombBossClip;
mainCamera.audio.Play();
mainCamera.audio.loop = true;
var boss : GameObject = Instantiate(bombermanBossPrefab, transform.position, transform.rotation);
bossToSpawn = false;
}

}

function FindGameObjectsWithLayer (layer : int) : GameObject[] {
var goArray = FindObjectsOfType(GameObject);
var goList = new System.Collections.Generic.List.();
for (var i = 0; i < goArray.Length; i++) {
if (goArray*.layer == layer) {*
goList.Add(goArray*);*
}
}
if (goList.Count == 0) {
return null;
}
return goList.ToArray();
}

You should not be calling “FindGameObjectsWithLayer” every frame in Update; nor recreating an array of the enemies every frame.

Instead, just keep track of the number of enemies with an int; and make the script on the enemy increase it by one when it wakes and decrease it by one when it dies.