# Weird problem: Spawning units inside a box

I’m making an asteroid belt generator. I’m trying to make it so that you set the size of the spawn space using a cube with no renderer.

Everything spawns and works other than the fact that they will only spawn around the edge of the cube.

I’m guessing I’m just not thinking of something in the math I did or there may be some nuance I’m not thinking of. Here is the code:

``````var numberOfAsteroids : int;

var asteroidPrefabs : GameObject[];

function Awake(){
CreateBelt();
}

function CreateBelt(){
var sizex = collider.bounds.size.x;
var sizey = collider.bounds.size.y;
var sizez = collider.bounds.size.z;

var currentPos = transform.position;

for(i = 0; i <= numberOfAsteroids; ++i){
var position = Vector3(Random.Range(currentPos.x - (sizex/2),currentPos.x + (sizex/2)),
Random.Range(currentPos.y - (sizey/2),currentPos.y + (sizey/2)),
Random.Range(currentPos.z - (sizez/2),currentPos.z + (sizez/2)));

var newAsteroid = Instantiate(asteroidPrefabs[Random.Range(0,asteroidPrefabs.length)], position, transform.rotation);

}
}
``````

I thought that should take the current position, find the min and max for each value, pick a random number. That for x,y,z. Then spawn the object, but there is a disconnect somewhere and I’d appreciate help figuring that out.

Just a first guess, but perhaps your collider is pushing them out via physics?

You could try disabling the collider to see if that’s the case (leaving it available for sizing, but removing it from the physics stuff).
Alternatively, try logging out your positions to double check that you’re not just being hit by unlucky random values.

perhaps try explicitly casting to a float?
something like changing this:

``````Vector3(Random.Range(currentPos.x - (sizex/2),currentPos.x + (sizex/2)),
``````

to this:

``````Vector3(Random.Range((float)(currentPos.x - (sizex/2)),(float)(currentPos.x + (sizex/2))),
``````

also fwiw, Bounds.extents is half the size of the…well…half the size of the size