Can't Find Optimization Problem in My Scripts

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

Use Unity’s built-in profiler to find out exactly what is taking the time in each frame.

it’s the Instantiate that happens every 2.2s in the coroutine. Also the point of an object pool is so you do not have to continuously instantiate new objects.

Try running Profiler to see what is causing the problem. Also as i saw on your script you are using foreach loop something that according to this is slowing the CPU. What about your sprites are you using one sprite for every element on your scene or is the resolution too high? A screenshot of Profiler would be great to understand in depth what causes the lag.

Check out Unity Documentation for more tips: Here
OR
Check out this blog as it has some good tips: Here

Finally figured out was was wrong!!! Had nothing to do with my scripts at all… I must have been messing with some project settings and turned VSync off. Now that I turned it back on it’s smooth as butter. Thanks to all of you who were willing to offer your help!