Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by temporaryman1 · Feb 20, 2013 at 10:59 PM · performanceframerate

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 :).

alt text

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();
 }
screen shot 2013-02-20 at 22.05.03.png (57.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image temporaryman1 · Feb 21, 2013 at 12:40 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation of terrain causes drastic performance drop (fps) 0 Answers

Opening profiler increases FPS 1 Answer

Why are small and basic particle so expensive? 1 Answer

unity cant run my movement code smoothly 0 Answers

Total memory allocated increases indefinately from .80GB untill crash 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges