x


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

    } 


}
more ▼

asked Dec 28 '09 at 12:52 PM

Jaywalker gravatar image

Jaywalker
514 15 21 30

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Dec 28 '09 at 03:12 PM

Mortim gravatar image

Mortim
709 3 7 18

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.

Dec 28 '09 at 03:26 PM Mortim

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 instead of instantiating where i hit something.

Dec 28 '09 at 03:56 PM Jaywalker

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

Dec 28 '09 at 06:13 PM Ashkan_gc

This was very helpful, thanks a lot !

May 29 '12 at 09:06 PM davvilla
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 28 '09 at 02:57 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

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.

Dec 28 '09 at 03:53 PM Jaywalker

by the way, you can seen the build here: http://www.theoddworks.com/?p=358

Dec 28 '09 at 03:57 PM Jaywalker
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2083
x1253
x662

asked: Dec 28 '09 at 12:52 PM

Seen: 4336 times

Last Updated: May 29 '12 at 09:06 PM