- Home /
Instantiating lowers framerate
My game is a tron-like game in which there is a trail behind the player and if another player walks into it they die. To do this I instantiate invisible cubes behind the player. This works fine but after a while the framerate starts to get lower and lower if the player is moving. If the player is not moving the frame rate goes back to normal. Is there any other way I could instantiate the cubes without lowering the framerate lowering? Or having a colidable trail?
void Update() {
if(rigidbody.velocity.magnitude > 7){
Network.Instantiate(collidie, transform.position + -transform.forward, transform.rotation, 0);
}
}
Can't you destroy the cubes gradually after a certain time interval ?
Ins$$anonymous$$d of keying off of speed (it makes one every frame, if you go fast enough?) Why not use distance from last place you spawned one? That would help keep the number down. They can even have small gaps between them (too small for other players to go through.)
Answer by Tanshaydar · Oct 09, 2014 at 12:57 PM
I am guessing you are using a prefab (collidie), add a script into it,
void Awake() {
Destory(gameObject, 5);
}
this will destroy your cube after 5 seconds, and your memory won't be filled with old objects.
If you create them too fast, without a time limit between two instantiate time, you need to add that too.
Lastly, you may want to look into polling.
I need the cubes to stay there. The problem isn't that there's too many cubes it's that the script is getting overloaded. Thanks anyway!
Answer by Kiwasi · Oct 09, 2014 at 11:24 PM
Instantiate is a slow, expensive function. Get used to it. Destroy will also cause you problems.
The best solution is object pooling. Google it. Or check out some of the premade scripts on this answer.
http://answers.unity3d.com/questions/765574/can-someone-just-post-a-generic-object-pool-script.html