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 dan5071 · Feb 26, 2016 at 10:57 PM · stutterobject poolchoppy

Object Pooling Causing Stutter

Hi everyone. I've been working on a game with a space ship that dodges asteroids, and I finally got everything running smoothly on my android device. However, it seems that my game has inexplicably started to hiccup every few seconds. I narrowed down the issues to my object pooling and generator script, but I can't seem to figure out what is causing the lag. Here's hoping a fresh set of eyes will help. I created an entirely new project with all the default unity settings (except of course I changed the platform to Android and changed the bundle ID). In this new project, I have only my generator and object pooling related scripts and as expected, there is a hiccup in the movement every 2-3 seconds. Below are all the relevant scripts. FYI, the rigidbody component of the asteroid prefab is set to "isKinematic."

ObjectPooler.cs - attached to an empty game object.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ObjectPooler : MonoBehaviour
 {
     public GameObject pooledObject;
     public int poolSize;
 
     private List<GameObject> _pool;
     private Transform _poolTransform;
 
     void Awake()
     {
         // Initialize private variables.
         _pool = new List<GameObject>();    // Initialize the pool list.
         _poolTransform = transform;        // Cache a reference to the pool's transform.
     }
 
     void Start()
     {
         // Initialize the pool by adding some instances of our prefab.
         for( int i = 0; i < poolSize; i++ )
         {
             addToPool();
         }
     }
 
     public GameObject GetPooledObject()
     {
         // Cycle through all of our pooled objects.
         foreach( GameObject go in _pool )
         {
             // If we find an inactive object in our pool...
             if( !go.activeInHierarchy )
             {
                 go.SetActive( true );    // Set the object to active.
                 return( go );            // Return the game object.
             }
         }
 
         // If we didn't find an available object in our pool, instantiate a new one.
         GameObject newObject = addToPool();
         newObject.SetActive( true );
         return( newObject );
     }
 
     private GameObject addToPool()
     {
         GameObject go = Instantiate( pooledObject );
         go.transform.parent = _poolTransform;
         go.SetActive( false );
         _pool.Add( go );
         return( go );
     }
 }
 

Generator.cs - attached to an empty game object.

 using UnityEngine;
 using System.Collections;
 
 public class Generator : MonoBehaviour
 {
     public ObjectPooler asteroidPool;    // The object pooler designed to generate asteroids.
 
     private float _createPosX;    // Horizontal position at which obstacles are generated.
     private float _startDelay;    // Time to wait before starting to generate obstacles.
 
     void Awake()
     {
         // Initialize private variables.
         _createPosX = 15.0f;
         _startDelay = 1.0f;
     }
 
     void Start()
     {
         // Begin the coroutines that generate obstacles.
         StartCoroutine( GenerateAsteroids() );
     }
     
     IEnumerator GenerateAsteroids()
     {
         // Delay generation in the beginning of the game.
         yield return new WaitForSeconds( _startDelay );
 
         // Loop forever.
         while( true )
         {
             // Randomize attributes of our new asteroid.
             float posY = Random.Range( -5.0f, 5.0f );    
             float speed = Random.Range( 3.0f, 5.0f );
             float angVel = Random.Range( -50.0f, 50.0f );
 
             // Select a game object from our object pool and set it to active.
             GameObject newAsteroid = asteroidPool.GetPooledObject();
 
             // Cache references to the new game object's transform and rigidbody components.
             Rigidbody2D rb = newAsteroid.GetComponent<Rigidbody2D>();
             Transform newTransform = newAsteroid.transform;
 
             // Apply the appropriate speed, position, etc.
             newTransform.position = new Vector2( _createPosX, posY );
             rb.velocity = Vector2.left * speed;
             rb.angularVelocity = angVel;
 
             // Wait a while before generating a new asteroid;
             yield return new WaitForSeconds( 2.2f );
         }
     }
 }
 

DestroyArea.cs - attached to an empty game object with a box collider sitting at the edge of the screen.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyArea : MonoBehaviour
 {
     void OnTriggerEnter2D( Collider2D col )
     {
         col.gameObject.SetActive( false );
     }
 }

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

Answer by swampman860912 · Nov 22, 2016 at 07:08 PM

Did you ever get an answer on this or figure it out? I am having the same issue. Its like the physics lags bad when it comes in contact with a collision object.

Comment
Add comment · 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

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

50 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 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

Test simulation stutters on keyup/keydown so badly I can't test anything. 0 Answers

5.3.2 FPS problem 1 Answer

Can someone post a generic object pool script? 4 Answers

Instantiate Objects in Pattern using UI Slider 1 Answer

Camera Slerp rotation Jitter / Stutter when in a hierarchy with a CharacterController parent 0 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