Instantiate causes bumpiness

Hey,

I am using Instatiate() to instantiate new ai cars close to the player when the old ones are too far away from the player. The problem is that each time a new car is created, my game will stop for a couple of frames and then take on again. My script is kind of like this:

var aiCars : GameObjects = new GameObjects[5];
var material : Material;

function SpawnCar()
{
GetSpawnPoints();
var random : Random.Range(0,aiCars.length - 1);
var spawnCar : GameObject = Instantiate(aiCars[random],waypoint.position,Vector3.zero);
var materialrandom : Random.Range(0,material.length - 1);
spawnCar.material = material[materialrandom];
}

as GetSpawnPoint just compares the distance between 200 points, I don’t think that it causes the problem. So is there any way to instantiate a car faster then with Instantiate() ?

I don’t care if the loading time at the beginning is longer, as long as the gameplay is fluent on the iPhone (4S and better)

2 Answers

2

You could try to use gameobject pooling instead of creating new objects every time. Watch the video :wink:

hmm the problem I have with pooling is that I kind of need to reset my cars to their default settings as they need to restart with their Start() functions. I tried it out anyways using “PrefabUtility.ResetToPrefabState” but I am getting NaN errors and I don’t really know how to fix those as I am not able to find the source of the problem.