- Home /
SpawnEnemies script slows the game significantly after just 40 or so?
My game contains about 500 trees (my own preFabs, not Unity trees) each of which Instantiate()s an enemy every few minutes or so. After about 40 enemies have been spawned, the game slows considerably, and gets worse from there. This isn't to do with the enemies, btw-- if I simply place several few hundred enemies into the scene everything works fine, so it must be this script.
This is the script on the tree. As you can see, it's very sparse, not sure what's slowing the game down. I also don't know why the number of enemies would make this script slower, since it doesn't store them... Thoughts?
public GameObject creature;
public Vector3 displacement = new Vector3(0,0,0);
public float spawnPeriod = 300.0f;
public float firstSpawnPeriod;
bool readyToSpawn = true;
bool first;
void Start()
{
first = true;
firstSpawnPeriod = Random.Range(2.0f, spawnPeriod);
}
void Update ()
{
if (readyToSpawn)
{
Spawn();
StartCoroutine(Delay());
}
}
IEnumerator Delay()
{
if (first)
{
readyToSpawn = false;
yield return new WaitForSeconds(firstSpawnPeriod);
readyToSpawn = true;
first = false;
}
else
{
readyToSpawn = false;
yield return new WaitForSeconds(spawnPeriod);
readyToSpawn = true;
}
}
void Spawn()
{
Instantiate(creature, transform.position+displacement, transform.rotation);
}
[SOLVED] turns out the movement script on the enemies was in fact the problem. The script loops over other gameobjects in the scene, which is why it ran with no problem when I didn't have trees in the scene. I've changed that code and it now runs smoothly with up to 200 enemies. I'll create a pool as suggested so that this number isn't exceeded. Thanks for the help!
Answer by highpockets · Nov 28, 2020 at 04:38 PM
If this script is on 40 trees and every 6 minutes you instantiate 40 prefabs at the same time you likely notice the major hit when the instantiation happens. Destroying and instantiating takes a fair amount of resources of the CPU up. You might want to look at object pooling to cache/reuse the enemies when you need them instead of instantiating and destroying every time.
Object pooling design pattern is the best answer!
Thanks for the reply! I don't think this is the issue, since I specifically set the trees to NOT spawn all at once by initializing a random first spawn time on line 11: firstSpawnPeriod = Random.Range(2.0f, spawnPeriod)
The slowdown is also not once every 6 $$anonymous$$utes, but rather a gradual increase. I looked into pooling and will probably implement it if I can't find the root of the issue, though it seems to me pooling would be helpful to avoid the instantaneous slowdown of an Instantiate() call, which is not what I'm experiencing :/
I would also go with the object pooling suggestion. But its usually handy to find the problem first. And that you can see in the profiler. Go to Windows, Analysis, Profiler. And then look at the peaks and do a "Deep Profile" at that point, so you can see all relevant information. Here you can see the steps that you can take; https://www.youtube.com/watch?v=fROTtgZK-Zs
Sorry, I missed that Random.Range line, but my answer would have been similar besides the same spawn time. Definitely a good idea to take a look at the profiler as there could be many other factors