Weird error

This script makes a prefab spawn at a random object, but I get a weird error

var prefab : GameObject;
var spawn1 : GameObject;
var spawn2 : GameObject;
var spawn3 : GameObject;
var spawn4 : GameObject;
var spawn5 : GameObject;
var spawn6 : GameObject;
var spawn7 : GameObject;
var spawn8 : GameObject;
var spawn9 : GameObject;
var spawn10 : GameObject;

function Start () {
SpawnerID = Random.Range(1, 10);

if(SpawnerID == 1){

Instantiate(prefab, spawn1.Transform, Quaternion.identity);

}

if(SpawnerID == 2){

Instantiate(prefab, spawn2.Transform, Quaternion.identity);

}

if(SpawnerID == 3){

Instantiate(prefab, spawn3.Transform, Quaternion.identity);

}

if(SpawnerID == 4){

Instantiate(prefab, spawn4.Transform, Quaternion.identity);

}

if(SpawnerID == 5){

Instantiate(prefab, spawn5.Transform, Quaternion.identity);

}

if(SpawnerID == 6){

Instantiate(prefab, spawn6.Transform, Quaternion.identity);

}

if(SpawnerID == 7){

Instantiate(prefab, spawn7.Transform, Quaternion.identity);

}

if(SpawnerID == 8){

Instantiate(prefab, spawn8.Transform, Quaternion.identity);

}

if(SpawnerID == 9){

Instantiate(prefab, spawn9.Transform, Quaternion.identity);

}

if(SpawnerID == 10){

Instantiate(prefab, spawn10.Transform, Quaternion.identity);

}

}

I get this:

It isn’t line 60 every time, it does one of the Instantiate lines

It shouldn’t be spawn#.Transform, it should be spawn#.transform.position

Also if you would use an array your script could be something like 8 lines long and would be more flexible when you later decide to change the number of spawn points.

Your code would be far shorter and simpler if you used an array instead of a bunch of separate variables.

–Eric

also if you would use an array your script could be something like 8 lines long and would be more flexible when you later decide to change the number of spawn points.

edit: as i have written more it doesnt matter that eric was first ;).

And yet I beat you all :stuck_out_tongue:

It must be getting late, can’t believe I missed that…

With arrays it would look like this:

var prefab : GameObject;
var spawnPoints : Transform[];

function Start () {

    var spawnerID : int = Random.Range (1, 10);

    Instantiate (prefab, spawnPoints[spawnerID].position, Quaternion.identity);

}

I did that now I have this error:

Alright, I’m not the best at this, but ill check that out

Have you assigned the prefab and spawnPoints via the inspector?

Not quite; it should be “Random.Range (0, spawnPoints.Length)”

–Eric

Yeah, but your script worked, thanks! Do you know how to get it so it will do this every 11 seconds?

That’s what I get for copy+paste…

var prefab : GameObject;
var spawnPoints : Transform[];

function Start () {
    
    InvokeRepeating ("Spawn", 11, 11);

}

function Spawn () {

    var spawnerID : int = Random.Range (1, spawnPoints.Length);

    Instantiate (prefab, spawnPoints[spawnerID].position, Quaternion.identity);

}

Dman, you are amazing, thank you so much for your help I have learned so much just from this! You’re awesome