Error : Transform.parent not working properly.

I have a script attached to my “Land” GameObject which spawn obstacles at different random co-ordinates, this is the main script.

var cube : GameObject;

function Start () {
var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
}

it works properly, but i want to make these obstacles child of their respective “Land”
so i added this to the script and finally it becomes,

var cube : GameObject;

function Start () {
var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
newCube.transform.parent = gameObject.transform;
}

but the problem is it starts spawning more than 1 obstacle per land, which is un-intended,

here are the screenshots,

Before adding newCube.transform.parent = gameObject.transform;

[][1]

After Adding it.

[][2]

i cant figure out the problem itself, why it duplicates my blocks.

Note :- One Land Object Spawns one Block, and there are 6-7 land blocks in the above images.

[1]:
[2]: http://

Instatiate transforms, not gameobjects :slight_smile:

So the prefab property ‘cube’ should be a transform!
new code:

 var cube : Transform;

 function Start () {
 var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
 newCube.parent = gameObject.transform;
 }