I have been trying for weeks to get my project running smoothly. It’s as simple as it gets. I have set up an object pool and generator so that a few prefabs will move from right to left on the screen at random velocities. No matter what I try, I still get a lot of lag. It must be something simple that I’m missing. It’s a simple 2D game with no more than 5 sprites moving at one time. It was working perfectly until a little while ago, and now for no reason it seems to lag terribly. I don’t even think I changed these scripts. PLEASE if you have any idea why, let me know. Note that I have a box collider at the left edge of the screen (set as trigger) to detect when to disable the objects. I didn’t include that because its obvious how it should be done.
Generator.cs
using UnityEngine;
using System.Collections;
public class Generator : MonoBehaviour
{
public ObjectPooler asteroidPool;
private float _createPosX;
private float _startDelay;
void Awake()
{
_createPosX = 15.0f;
_startDelay = 1.0f;
}
void Start ()
{
StartCoroutine( GenerateAsteroids() );
}
IEnumerator GenerateAsteroids()
{
// Delay generation in the beginning of the game.
yield return new WaitForSeconds( _startDelay );
GameObject newAsteroid;
float posY;
float speed;
float angVel;
// Loop forever
while( true )
{
// Randomize attributes of our new asteroid.
posY = Random.Range( -5.0f, 5.0f );
speed = Random.Range( 3.0f, 5.0f );
angVel = Random.Range( -50.0f, 50.0f );
// Select a prefab from our object pool and set it to active.
newAsteroid = asteroidPool.GetPooledObject();
newAsteroid.SetActive( true );
// Cache a reference to the new asteroid's rigidbody component.
Rigidbody2D rb = newAsteroid.GetComponent<Rigidbody2D>();
// Cache a reference to the new asteroid's transform component.
Transform newTransform = newAsteroid.transform;
// Apply the appropriate speed, position, etc. to the asteroid.
newTransform.position = new Vector2( _createPosX, posY );
rb.velocity = Vector2.left * speed;
rb.angularVelocity = angVel;
// Generate a new asteroid after the given amount of time.
yield return new WaitForSeconds( 2.2f );
}
}
}
ObjectPooler.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPooler : MonoBehaviour
{
public GameObject pooledObject;
public int pooledAmount;
private List<GameObject> _pooledObjects;
private Transform _poolerTransform;
void Awake()
{
_pooledObjects = new List<GameObject>();
_poolerTransform = transform;
}
void Start()
{
for( int i = 0; i < pooledAmount; i++ )
{
GameObject go = Instantiate( pooledObject );
go.transform.parent = _poolerTransform;
go.SetActive( false );
_pooledObjects.Add( go );
}
}
public GameObject GetPooledObject()
{
// Cycle through all of our pooled objects...
for( int i = 0; i < _pooledObjects.Count; i++ )
{
// If we find an object in our pool that is inactive...
if( !_pooledObjects*.activeInHierarchy )*
-
{*
-
// Return this game object.*
return pooledObjects*;
_ }*
* }*
* // If we didn’t find an available object in our pool, instantiate a new one.*
* GameObject go = Instantiate( pooledObject );*
* go.transform.parent = poolerTransform;
_ go.SetActive( false );*
* pooledObjects.Add( go );
_ return( go );*
* }*
* public void ResetPool()*
* {*
* foreach( GameObject go in pooledObjects )
_ {*
* go.SetActive( false );*
* }*
* }*
}