• 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 /
  • Help Room /
avatar image
0
Question by JayFitz91 · Sep 08, 2015 at 09:54 PM · optimizationgeneric

Code optimization?

My code below code works fine, but the issue is, I use this exact same code layout for 6 more pooled items in my game (bullets, enemies etc) So my manager class contains a lot of repetitive variables and code like I have above, is there a more efficient way I can portray this code? Maybe using a generic class and making a call when needed or something similar?

EDIT

A better example of what I mean, I have crystals, enemies and ammo, all being pooled at the beginning, set active and being stored in a parent, I am doing it for each object. Is the a better way to optimize this so I only need to use it once?

 //
 //Objects to spawn
 //
 
 //The object to be spawned
 public GameObject[] crystal;
 
 public GameObject[] swarmEnemy;
 
 //The object to be spawned
 public GameObject[] ammo;
 
 //
 //Lists
 //
 
 //The list which holds all the crystals
 public List<GameObject> crystals;
 
 public List<GameObject> swarmEnemies;
 
 //The list which holds all the ammo
 public List<GameObject> ammoList;
 
 
 //
 //Parents
 //
 
 public GameObject crystalParent;
 
 public GameObject swarmEnemyParent;
 
 public GameObject ammoListParent;
 
 void Start()
 {
     crystals = new List<GameObject>();
     swarmEnemies = new List<GameObject>();
     ammoList = new List<GameObject>();
 
     for (int i = 0; i < pooledAmount; i++)
     {
         GameObject crystal1 = (GameObject)Instantiate(crystal[0]);
         GameObject crystal2 = (GameObject)Instantiate(crystal[1]);
         GameObject crystal3 = (GameObject)Instantiate(crystal[2]);
         GameObject crystal4 = (GameObject)Instantiate(crystal[3]);
 
         crystals.Add(crystal1);
         crystals.Add(crystal2);
         crystals.Add(crystal3);
         crystals.Add(crystal4);
 
         for (int j = 0; j < crystals.Count; j++ )
         {
             crystals[j].SetActive(false);
             crystals[j].transform.parent = crystalParent.transform;
         }
 
         GameObject enemy1 = (GameObject)Instantiate(swarmEnemy[0]);
         GameObject enemy2 = (GameObject)Instantiate(swarmEnemy[1]);
 
         swarmEnemies.Add(enemy1);
         swarmEnemies.Add(enemy2);
 
         for (int j = 0; j < swarmEnemies.Count; j++)
         {
             swarmEnemies[j].SetActive(false);
             swarmEnemies[j].transform.parent = swarmEnemyParent.transform;
         }
 
         GameObject ammo1 = (GameObject)Instantiate(ammo[0]);
         GameObject ammo2 = (GameObject)Instantiate(ammo[1]);
 
         ammoList.Add(ammo1);
         ammoList.Add(ammo2);
 
         for (int j = 0; j < ammoList.Count; j++)
         {
             ammoList[j].SetActive(false);
             ammoList[j].transform.parent = ammoListParent.transform;
         }
     }
 }
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

2 Replies

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

Answer by LuisCRSousa · Sep 08, 2015 at 10:14 PM

 //The object to be spawned
      public GameObject[] crystal;
  
      //The list which holds all the crystals
      public List<GameObject> crystals;
  
      //The parent which will store the crystals
      public GameObject crystalParent;
      
      void Start()
      {
          crystals = new List<GameObject>();
  
          for (int i = 0; i < pooledAmount; i++)
              for (int k = 0; k < crystal.lenght; k++){
                  crystals.Add((GameObject)Instantiate(crystal[k]));
                  crystals[k].SetActive(false);
                  crystals[k].transform.parent = crystalParent.transform;
              }
      }
Comment
Add comment · Show 4 · 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 JayFitz91 · Sep 08, 2015 at 10:38 PM 0
Share

@LuisCRSousa Thanks for the code, unfortunately your code was only adding 4 items to the parent, it needed one more nested for loop (which im not sure is good or not) but that is another question.

The issue though was not with adding the crystals, I shall update my question to provide a better example

avatar image LuisCRSousa JayFitz91 · Sep 08, 2015 at 11:19 PM 0
Share

$$anonymous$$y code wasn't adding just 4 items. I was adding crystal.lenght items.

The same thing with your updated code:

     void Start()
     {
         crystals = new List<GameObject>();
         swarmEnemies = new List<GameObject>();
         ammoList = new List<GameObject>();
 
         for (int i = 0; i < pooledAmount; i++)
         {
             for (int k = 0; k < crystal.Length; k++)
             {
                 crystals.Add((GameObject)Instantiate(crystal[k]));
                 crystals[k].SetActive(false);
                 crystals[k].transform.parent = crystalParent.transform;
             }
 
             for (int k = 0; k < swarmEnemy.Length; k++)
             {
                 swarmEnemies.Add((GameObject)Instantiate(swarmEnemy[k]));
                 swarmEnemies[k].SetActive(false);
                 swarmEnemies[k].transform.parent = swarmEnemyParent.transform;
             }
 
             for (int k = 0; k < ammo.Length; k++)
             {
                 ammoList.Add((GameObject)Instantiate(ammo[k]));
                 ammoList[k].SetActive(false);
                 ammoList[k].transform.parent = ammoListParent.transform;
             }
         }
     }

Note: This code can be optimized.

avatar image JayFitz91 LuisCRSousa · Sep 08, 2015 at 11:23 PM 0
Share

It does add 4 items, because the variable crystal has 4 objects in it. So this code only adds 4 items, my object pool is of size 20 so it should add 80 crystals to the pool, not just 4, the other 76 stay active and do not parent to the object. The following code adds all 80 crystals to the parent and disables them:

 for (int i = 0; i < pooledAmount; i++)
         {
             for (int j = 0; j < crystal.Length; j++)
             {
                 crystals.Add((GameObject)Instantiate(crystal[j]));
 
                 for (int k = 0; k < crystals.Count; k++)
                 {
                     crystals[k].SetActive(false);
                     crystals[k].transform.parent = crystalParent.transform;
                 }
             }
         }

Show more comments
avatar image
0

Answer by JayFitz91 · Sep 08, 2015 at 11:29 PM

I'm not sure how much more optimized this is but it certainly looks better from a code point. I created a function InstantiateAndAdd

 void InstantiateAndAdd(GameObject[] objectArray, List<GameObject> objectList, GameObject objectParent)
     {
         for (int j = 0; j < objectArray.Length; j++)
         {
             objectList.Add((GameObject)Instantiate(objectArray[j]));
 
             for (int k = 0; k < objectList.Count; k++)
             {
                 objectList[k].SetActive(false);
                 objectList[k].transform.parent = objectParent.transform;
             }
         }
     }

I then replaced all code I had before with the function I made and added the variables needed like so:

 void Start()
     {
         crystals = new List<GameObject>();
         smashedCrates = new List<GameObject>();
         slimeBalls = new List<GameObject>();
         swarmEnemies = new List<GameObject>();
         smashedAmmoCrates = new List<GameObject>();
         ammoList = new List<GameObject>();
 
         for (int i = 0; i < pooledAmount; i++)
         {
             InstantiateAndAdd(crystal, crystals, crystalParent);
             InstantiateAndAdd(swarmEnemy, swarmEnemies, swarmEnemyParent);
             InstantiateAndAdd(ammo, ammoList, ammoListParent);
             InstantiateAndAdd(smashedCrate, smashedCrates, smashedCrateParent);
             InstantiateAndAdd(smashedAmmoCrate, smashedAmmoCrates, smashedAmmoCrateParent);
             InstantiateAndAdd(slimeBall, slimeBalls, slimeBallParent);
         }
     }

Thanks to @LuisCRSousa for some help with the matter

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 LuisCRSousa · Sep 08, 2015 at 11:33 PM 0
Share

Oh, i was doing just that :P

Nice ;)

     void Start()
     {
         crystals = new List<GameObject>();
         swarmEnemies = new List<GameObject>();
         ammoList = new List<GameObject>();
 
         for (int i = 0; i < pooledAmount; i++)
         {
             pool(crystal, crystals, crystalParent);
             pool(swarmEnemy, swarmEnemies, swarmEnemyParent);
             pool(ammo, ammoList, ammoListParent);
         }
     }
 
     void pool(GameObject[] spawnedObjects, List<GameObject> list, GameObject parent)
     {
         for (int j = 0; j < spawnedObjects.Length; j++)
         {
             list.Add((GameObject)Instantiate(spawnedObjects[j]));
 
             for (int k = 0; k < list.Count; k++)
             {
                 list[k].SetActive(false);
                 list[k].transform.parent = parent.transform;
             }
         }
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimize the standard shaders texture slots? 0 Answers

IOS Game Optimization? 1 Answer

I'm having a little problem with the images sizes 1 Answer

Optimisation problem (Load/Instantiate) 0 Answers

Android Performance Issues 1 Answer


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