- Home /
Number of objects in scene and performance
Hey all,
In my game there are a lot of objects that can be destroyed when you shoot them. In spirit with the season i made a version with the text "Seasons greetings" all made out of destructible prefabs. The frame rate doesn't get a hit, but when i destroy a couple there is a frame drop for an instant.
Is there some kind of limit to the number of unique objects i have in a scene?
Thanks a lot! And best wishes!
This is the code on my prefabs:
var object : GameObject; var explosionSphere : GameObject; var objectTrigger : GameObject; var destroyPS : Transform; var objectLife = 5; var explosive = false;
function OnTriggerEnter (objectTrigger : Collider) { // Only react on bullets if (objectTrigger.tag == "Bullet") { objectLife -= 30; if (objectLife < 0) { if(explosive == true) { Instantiate(explosionSphere, transform.position, transform.rotation); } Instantiate(destroyPS, transform.position, transform.rotation); Destroy (object); GuiScript.Points += 10; }
}
}
Answer by Mortim · Dec 28, 2009 at 03:12 PM
Instantiating many objects in a short time lapse can result in a loss of performance. When possible, you should try to recycle objects rather than creating new ones.
I had a similar problem in my game, where I used to instantiate particle systems to simulate explosions and hit effects. I resolved it by creating a pool of explosions at loading, which is implemented by a circular list. When I need to create a new explosion, I search for the next explosion in the list, position it where I need and enable its particle emitter . When I need to "destroy" an explosion, I just disable it.
This principle can be applied for most dynamic game object, like shoots, enemies, hit effects and so on. Depending on the type of game you are making, recycling objects can result in a huge improvement on fps.
Of course, in order to recycle objects, you need to know the maximum number of objects of a given type which may be active at the same time, and create a pool of at least that number.
After further thoughs, it seems unlikely that your problem is due to not recycling objects. If you have a frame drop with only a couple of explosions, the issue is probably lying in the content of the explosions themselves, as Ashkan suggests above. However, recycling objects is always a good thing when it's posible.
Hey! I actually instantiate quite a lot, the impact explosion creates a sphere that expands and sets off all the cubes it touches (about 8 max). Ill look into moving 1 particle system around ins$$anonymous$$d of instantiating where i hit something.
instantiate is a complex method and i think it is a factory method and do many things but using this approach can lead to a less readable and maintainable code. i don't how much performance it can improve but if it does and you need it so what is the reason to don't use. less readable and reusable code is better that unusable code. :D
Answer by Ashkan_gc · Dec 28, 2009 at 02:57 PM
there is no object limit. there might be different reasons for FPS drop. first of all you might have heavy rendering with many particles in your explosions. if you use rigidbodies in any of the objects that you create after destruction of this one. they might eat alot of resources. if you have the unity pro you can get more information by using the profiler. as a guideline try to use as lightweight code as possible and keep the number of particles as low as possible. the problem might be a rendering buttle neck or a CPU buttle neck or even a RAM or VRAM buttle neck if you create high polygon meshes with big textures after destroying each object. so there are many possibilities. give more descriptions.
Thanks a lot! I've been checking the profiler and it seems that every time i spawn the particle system it eats 13.1ms. So I'm looking into why this is.
by the way, you can seen the build here: http://www.theoddworks.com/?p=358