Edit:
After some “investigating” following @sacredgeometry and @TimBur responds I looked into profiler and found out that the problem is actually in Polygon Collider 2D that is on the sprite of my cave so the Object Pooling is not really the problem, Polygon Collider 2D is.
This is my sprite for the cave map:
[173398-cavebot9.png*|173398]
And this is that sprite with Polygon Collider 2D:
[173399-cavenesto.png*|173399]
So, does anyone know if there is any solution to this or Polygon Collider 2D is just to much for something like this? Anything else I can use to put collider 2D on something like this sprite?
Old question:
Hello, I made a simple 2d game where I have a Player character made out of few sprites and the terrain made of out two sprites that are making a map through Object Pooling.
When I disable Object Pooling script the game runs flawlessly but with the Object Pooling script enabled the game runs poorly because of the FPS drop. Before I start the game the FPS is fine, when I hit Play and Object Pooling starts FPS drops and when the game is over (Object Pooling gets disabled on game over) the FPS is fine again.
I only made Object Pooling because people told me not to use Destroy because that will slow my game and now Object Pooling is slowing my game.
The sprites that are in the Pool I made in Photoshop and simply put them in the project, I didn’t lower the quality of those sprites so I don’t know if that may be the problem?
Anyone know what am I doing wrong?
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CavePool : MonoBehaviour
{
public int cavePoolSize = 5;
public GameObject CavePrefab;
public float spawnRate = 3f;
public float caveMin = 2f;
public float caveMax = -2f;
private GameObject[] caves;
private Vector2 objectPoolPosition = new Vector2(-20f, -20f);
private float timeSinceLastSpawned;
private float spawnXPosition = 12f;
private int currentCave = 0;
void Start ()
{
caves = new GameObject[cavePoolSize];
for (int i = 0; i < cavePoolSize; i++)
{
caves *= (GameObject)Instantiate(CavePrefab, objectPoolPosition, Quaternion.identity);*
}
}
void Update ()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameManager.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0;
float spawnYPosition = Random.Range(caveMin, caveMax);
caves[currentCave].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentCave++;
if (currentCave >= cavePoolSize)
{
currentCave = 0;
}
}
}
}
*
*