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 Inan-Evin · Nov 28, 2014 at 05:30 PM · instantiateobjectperformancedecalpool

Object Pooling, have some question marks

So hi everyone. I have several questions about object pooling method. I want to use object pooling for handling my bullet decal objects. As you know, simply re-using pre instantiated objects instead of instantiating and destroying is much better way because of garbage collection. In my game, i want to create bullet decals using this method. I actually did it and i have some questions about it. First of all, for your better understanding here is my code :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ObjectPool : MonoBehaviour {
 
     public static ObjectPool currentScript;
     public bool willExpand;
     public GameObject concreteDecalPooled;
     public int pooledConcreteDecalAmount;
     List<GameObject> pooledConcreteDecals;
 
     void Awake()
     {
         currentScript = this;
     }
 
     void Start()
     {
         pooledConcreteDecals = new List<GameObject>();
         
         for(int i = 0; i < pooledConcreteDecalAmount; i++)
         {
             if(!isMultipleConcrete)
             {
                 GameObject objConcrete = (GameObject)Instantiate(concreteDecalPooled);
                 objConcrete.SetActive(false);
                 pooledConcreteDecals.Add(objConcrete);
             }
         }
     }
 
     public GameObject GetPooledObject()
     {
         for (int i = 0; i < pooledConcreteDecals.Count; i++)
         {
             if(!pooledConcreteDecals[i].activeInHierarchy)
             {
                 return pooledConcreteDecals[i];
             }
         }
 //JUST IGNORE THIS PART FOR NOW, I HAVE QUESTION ABOUT HERE
         if(willExpand)
         {
             GameObject obj = (GameObject)Instantiate(concreteDecalPooled);
             pooledConcreteDecals.Add(obj);
             return obj;
         }
 
 
         return null;
     }
 
 }


And in my decal script, i simply make my decals SetActive(false) after desired amount of seconds. Well I create like 50 decals at the beginning in my pool. But when i fire a weapon like machine gun, it look ridicilous because of the fact that after 50. bullet it takes the first decal and puts it in the latest hit position, so it does not look cool you know. Decals always changing positions in long period firing. To prevent this, if I increase the amount of seconds that defines when decals will be active, then no decals will be instantiated until time passes because we will be out of our 50 decaled list. Now the thing is, i put an if statement over the code, willExpand, which makes the pool to add 1 more decal each time when we are out of the list. In this way, when fire fired like 200 bullets, it looks okay, decals are realistic because we didn't re-use 150 of them, we just instantiated them. But the thing is, now i have 200 decals in my scene ? And it will continue to expand if i ever pass the limit.

So is this still an optimized way ? In maximum how many gameObjects can we keep in the scene as inactive. I am concerned because i think about using this method in different decal types, tracers, muzzles etc. Also I thought about this. If we are out of active decals in list, should I instantiate the extra decals in another prefab which has destroy script in it instead of setactive(false) ? I need someone to enlighten me, thanks in advanced :).

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

0 Replies

· Add your reply
  • Sort: 

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

26 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

Related Questions

Activating GameObject(s) is slow 2 Answers

IOS Object Pooling 0 Answers

Most performance friendly way to "unrender" a GameObject? 6 Answers

Arrow fires at wrong rotation 0 Answers

Putting prefab into scene messes with the transform origin? 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