Help with spawning an object in a random location

Hi

I’m new to this and I could need a little help with my script.
It’s supposed to spawn an object in a random location between the coordinates (1,0.5,1) and (29,0.5,29) and if that object gets destroyed it should spawn a new one in a new random location.
This is the script I have made. It seems that it only spawn the object in the location (0,0,0). A little help would be appreciated.

    #pragma strict

function Start () {
}

var gameobject : GameObject;
var location : Vector3 = Vector3(0,0.5,0);
var xaxis : int=15;
var zaxis : int=15;


function Update () {

var xaxis=Random.Range(1,29);
var zaxis=Random.Range(1,29);

var location : Vector3 = Vector3(xaxis, 0.5, zaxis);
if(transform.childCount < 1)spawn();
	

}
function spawn () {
var Instance : GameObject = Instantiate(gameobject, location, transform.rotation);
Instance.transform.parent = transform;
}

Your problem is that you are defining location, xaxis and zaxis all twice. Although all those variables are being changed, because they are being redefined as themselves, var Instance is simply interpreting it as what is defined outside of Update(), which is Vector3(0,0.5,0).

Basically delete your variable definitions of location, xaxis and zaxis outside of Update() and it should now work properly for you. :slight_smile:

Klep