Performance issue with instantiating

Hi, I’ve just began learning the ropes of Unity, and have been developing a simple asteroids clone as a practice project.

Here’s what I have after four days of fiddling around: http://koti.mbnet.fi/marwer/asteroids.html

WASD to control the ship, left mouse button to fire, mouse wheel to zoom in and out, middle button to focus camera on the ship and you can also scroll the screen like in an rts. Refresh if you get killed.

Notice how the fps drops for a few milliseconds when you destroy an asteroid? That’s my problem. I use the following script to spawn the asteroids after one has been destroyed:

function Update () {
	if (hitPoints <= 0){
		Instantiate(explosion, transform.position, transform.rotation);
		Destroy(gameObject);
		for (var i = 0; i < children; i++){
			Explode();
		}
	}
}

function Explode(){
	var pos = transform.position + Vector3(Random.Range(-30, 30), 0, Random.Range(-30, 30));
	var rot = Quaternion(Random.value*360, Random.value*360, Random.value*360, Random.value*360);
	Instantiate(child, pos, rot);
}

Is there something wrong with this or is instantiating just so slow? The asteroid model only has 300 triangles. What would be the best way to instantiate multiple objects in one frame at runtime?

Have you tried preloading the items? A model may need to be loaded onscreen by showing it to the camera at runtime. This has happened to me often.

Put a “loading” GUITexture onscreen while you pre-instantiate the models - this should fix any problems.

If anyone else has a better idea feel free :stuck_out_tongue:

The instantiated asteroids are the same models which are spawned when the game starts, so they should be preloaded before any asteroid is destroyed, if that’s what you meant.

How many objects are you spawning? Do these objects have any particular code in their Awake() or Start()? In my experience you can spawn hundreds of objects per second without problems, so I’m guessing your performance problem comes from something else which is triggered in relation to the event…

Currently destroying an asteroid creates three smaller ones in its place. Hmm, could it have something to do with them spawning within each other, or within the destroyed asteroid before it is removed?

Here’s all the code that goes in an asteroid:

Asteroid.js:

var initVelMin:float = 0;
var initVelMax:float = 100;
var children:int = 3;
var child:GameObject;
var hitPoints = 100;
var explosion:GameObject;

function Start(){
	rigidbody.velocity.z = Random.Range(initVelMin, initVelMax);
}

function Update () {
	if (hitPoints <= 0){
		Instantiate(explosion, transform.position, transform.rotation);
		Destroy(gameObject);
		for (var i = 0; i < children; i++){
			Explode();
		}
	}
}

function ApplyDamage(damage){
	if (hitPoints <= 0){
		return;
	}
	hitPoints -= damage;
}

function Explode(){
	var pos = transform.position + Vector3(Random.Range(-30, 30), 0, Random.Range(-30, 30));
	var rot = Quaternion(Random.value*360, Random.value*360, Random.value*360, Random.value*360);
	Instantiate(child, pos, rot);
}

XZPlaneLock.js:

function FixedUpdate () {
	transform.position.y = 0;
	transform.rotation.x = 0;
	transform.rotation.z = 0;
}

It sounds weird to me too, I’ve spawned more complicated objects than that more frequently than your asteroids without seeing the issue you’re getting.

Have you confirmed it’s the spawning’s fault by commenting out the Instantiate’s and seeing if the pause goes away?

You spawn an explosion and the asteroid, is one causing the lurch or are both contributing?

How many particles are in the explosion effect?

Is there any way the particles could be getting spawned multiple times? Admittedly looking at your code I don’t see how it could be, but you could tell easily by watching the inspector view while the game’s running.

Yes I’m quite sure it’s the asteroids spawning that causes it. It started when I added it and works if I comment it out. It also works fine if I spawn just one asteroid but the more I spawn them the longer the pause seems to get. The explosion effect is the one from the standard assets, I just changed the min and max sizes a bit.

It has to have something to do with the asteroid model after all. I switched the asteroids to spawn ships (which are models with >1800 tris btw) and it works fine.

I’m guessing you are using mesh colliders, which are slow. Replace those with primitive colliders, and the pause should go away.

–Eric

Yes, that worked. Thank you very much. Though the funny thing is that the ship uses a mesh collider too and still works. :stuck_out_tongue: