Need help with my spawner script

Hello folks!

Got a problem with a script that hopefully someone could help me out with.
The point of the script is to spawn platforms aligned perfectly next to each other, without any gap between. The “SpawnDistance” variable sets the distance on how often the platforms should spawn, for example if its set to 20, then they will spawn every 20 unit in unity.

It partly works, platforms with the same size (say 20 units) will spawn perfectly aligned. But at times they spawn with a gap between, so it looks like this: Link to imgur when it instead should look like this: Link to imgur. And i don’t know why this randomly happens. For it to work, all platforms need to be the same size, which they are. Any ideas?

var obj : GameObject[];
var player : Transform;
private var xmin = 0;
var SpawnDistance = 0;


function Spawn() {
	var temp = Mathf.FloorToInt(player.position.x/SpawnDistance);

	if (temp > xmin){
		for(var i = xmin; i < temp; i++){
			Instantiate (obj [Random.Range (0, obj.Length)], transform.position, Quaternion.identity); 
		}
			xmin = temp;
	}
}

looks like either floating point inprecision or where you getting transform.position?
I’m not sure what gameobject this is attached to.

anyways you need to just check before you feed it the position it’s a perfect whole number and it seems almost certain this isn’t.

what you want to do is basically (i dont know java)

IntTransform = (mathf.round(transform.position.x), mathf.round(transform.position.y),mathf.round(transform.position.z))

instaniate (.....,IntTransform,...);

alternately you can just round once and iterrate.

basically the problem here which you’ll be able to see through

debug.log (transform.position.x)

is x isn’t a perfect whole number and it’s being less than precise at times.

That should be the cause of your error so round it off and use that.