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 );
}
}