Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Emmsii · Nov 18, 2015 at 08:42 PM · c#object pool

FPS Slowly reduces over time with multi-object pool

I have two turret types in my game, a missile turret and a machine gun turret. They fire missiles and bullets respectively. The bullets have a trail renderer attached, the missile object has a particle trail and spawns an explosion on collision.

I've created a multi-object object pool to go easy on the multiple Instantiate/Destroy calls I would have to make with lots of projectiles.

Here's my object pool script: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class ObjectPool : MonoBehaviour {
 
     public static ObjectPool instance;
 
     public GameObject[] objectPrefabs;
     public int defaultPoolSize = 40;
 
     private Dictionary<string, List<GameObject>> objectPool;
     protected GameObject containerObject;
     
     void Awake(){
         instance = this;
     }
 
     void Start(){
         objectPool = new Dictionary<string, List<GameObject>> ();
         containerObject = new GameObject ("ObjectPool");
 
         foreach (GameObject obj in objectPrefabs) {
 
             List<GameObject> objects = new List<GameObject>();
 
             for(int i = 0; i < defaultPoolSize; i++){
                 GameObject newObj = Instantiate(obj);
                 newObj.transform.parent = containerObject.transform;
                 newObj.SetActive(false);
                 objects.Add(newObj);
             }
 
             objectPool.Add(obj.name, objects);
         }
     }
 
     public GameObject GetPooledObject(string name){
         List<GameObject> objs =  objectPool[name];
 
         foreach (GameObject obj in objs) {
             if(!obj.activeInHierarchy){
                 return obj;
             }
         }
 
         foreach (GameObject currentObj in objectPrefabs) {
             if(currentObj.name == name){
                 GameObject newObj = Instantiate(currentObj);
                 newObj.transform.parent = containerObject.transform;
                 newObj.SetActive(false);
                 objectPool[name].Add(newObj);
                 return newObj;
             }
         }
 
         return null;
     }
 
 }

Bullets and missiles have the following script attached:

Bullets:

 using UnityEngine;
 using System.Collections;
 
 public class BulletController : MonoBehaviour {
 
     public float speed = 25f;
     public float life = 5f;
     
     public ParticleSystem bulletHit;
     
     private Rigidbody rb;
     private TrailRenderer trailRenderer;
     private MeshRenderer mesh;
 
     void Awake(){
         rb = GetComponent<Rigidbody> ();
         trailRenderer = GetComponent<TrailRenderer> ();
         mesh = GetComponent<MeshRenderer> ();
     }
 
     private void OnEnable(){
         mesh.enabled = true;
         trailRenderer.enabled = true;
         rb.velocity = speed * transform.forward;
         Invoke ("Remove", life);
     }
     
     private void OnDisable(){
         CancelInvoke ();
     }
 
     private void Remove(){
         transform.rotation = Quaternion.identity;
         trailRenderer.enabled = false;
         mesh.enabled = false;
         rb.velocity = Vector3.zero;
         rb.angularVelocity = Vector3.zero;
         transform.position = transform.parent.position;
         Invoke ("WaitTime", 0.5f);
     }
 
     private void OnCollisionEnter(Collision other){
         Remove ();
     }
     
     private void WaitTime(){
         gameObject.SetActive (false);
     }
 }
 

Missiles:

 using UnityEngine;
 using System.Collections;
 
 public class MissileController : MonoBehaviour {
 
     public float speed = 30f;
     public float turnSpeed = 15f;
     public float life = 4f;
     public string explosionName;
 
     [HideInInspector]
     public Transform target;
 
     private ParticleSystem missileTrail;
     private float particleEmissionRate;
     private bool alive;
     private Rigidbody rb;
     private Light light;
     private MeshRenderer mesh;
 
     void Awake(){
         mesh = GetComponent<MeshRenderer> ();
         light = GetComponentInChildren<Light> ();
         rb = GetComponent<Rigidbody> ();
         missileTrail = GetComponentInChildren<ParticleSystem> ();
         particleEmissionRate = missileTrail.emissionRate;
     }
 
     private void OnEnable(){
         rb.isKinematic = false;
         mesh.enabled = true;
         light.enabled = true;
         alive = true;
         missileTrail.emissionRate = particleEmissionRate;
         missileTrail.transform.position = transform.position;
         missileTrail.transform.parent = transform;
         Invoke ("Remove", life);
     }
 
     private void OnDisable(){
         CancelInvoke ();
     }
 
     private void Remove(){
         target = null;
         mesh.enabled = false;
         light.enabled = false;
         alive = false;
         missileTrail.emissionRate = 0;
         transform.position = transform.parent.position;
         rb.isKinematic = true;
     }
 
     private void WaitForTrail(){
         gameObject.SetActive (false);
     }
 
     private void OnCollisionEnter(Collision other){
         GameObject explosion = ObjectPool.instance.GetPooledObject ("Explosion");
         explosion.transform.position = transform.position;
         explosion.SetActive (true);
 
         Remove ();
         Invoke ("WaitForTrail", 2f);    
     }
 
     void FixedUpdate () {
         if (target && alive) {
             transform.Translate(Vector3.forward * speed * Time.deltaTime);
             Quaternion rot = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), turnSpeed * Time.deltaTime);
             transform.rotation = rot;
         }
     }
 }

Bullets and missiles are instantiated with the following lines:

 GameObject bullet = ObjectPool.instance.GetPooledObject("Bullet");
 bullet.transform.position = t.position;
 bullet.transform.rotation = Quaternion.LookRotation(dir);
 bullet.SetActive(true);

The issue I'm getting is that my fps lowers over time, in the Unity editor and standalone .exe file. I've looked at the profiler and the WaitTime() & WaitForTrail() methods cause occasional spikes which seem to increase in duration over time. The overdraw seems to be high as well. alt text

untitled.png (232.0 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bullet prefab doesn't go to given target 0 Answers

Instantiate Objects in Pattern using UI Slider 1 Answer

Can I change which GameObject is being drawn from a pool via a public variable/the property inspector? 0 Answers

need help in making an object pooler 1 Answer

Null Reference Exception ONLY (sometimes) when loading the scene... 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