- Home /
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 :).
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[i].GetComponent(enemyBehaviour);
//Debug.Log("health is" + eneBehaviour.health);
if(enemies[i].transform.childCount == 0){
Destroy(enemies[i]);
var score : int = PlayerPrefs.GetInt("player score");
score += 5;
PlayerPrefs.SetInt("player score", score);
//Debug.Log("should destroy enemies[i]");
}
}
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.<GameObject>();
for (var i = 0; i < goArray.Length; i++) {
if (goArray[i].layer == layer) {
goList.Add(goArray[i]);
}
}
if (goList.Count == 0) {
return null;
}
return goList.ToArray();
}
Answer by Kiloblargh · Feb 20, 2013 at 11:49 PM
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.
using player prefs right? I realised I am calling FindGameObjectsWithLayer 3 times which is what was causing the whole problem. I even got my polycount down to 2k! Atleast it forced me to optimize hard. Anyway... Your answer has answered my question! Thankyou!