Instantiate position from four options.

Hi, so I have to have zombies spawn at one of four spawners. My problem is that I don’t know how to put in more than one location as the spawn locations. I tried doing

Instantiate(Zombie, (spawn1 || spawn2 || spawn3 || spawn4), etc...)

but that did not work. Is there any other way I can do this?

I like how you think, but unfortunately OR works with boolean arguments and thus are those spawnpoints converted to true/false.

1 Answer

1

This is quite simple just make an array which contains your possible spawn locations and pick one randomly. The code could look like this (c#):

public Transform[] spawnPoints;  //assign the spawnpoints in the inspector or during Start

void SpawnZombie(){
    int pick = Random.Range(0, spawnpoints.Length);  //select one spawnpoint randomly
    Instantiate (Zombie, spawnPoints[pick].position, spawnPoints[pick].rotation);
}

var spawnPosition = new Array (spawnOne.transform.position, spawnTwo.transform.position, spawnThree.transform.position, spawnFour.transform.position); Instantiate(zombie, (spawnPosition[Random.Range(0, 3)]) + Vector3((Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2, 0.5, (Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2), Quaternion.Euler(0,0,0)); For some reason in the Instantiate it is telling me that the (spawnPosition[Random.Range(0, 3)]) is an object and won't compile because it says that Object + Vector 3 is not allowed.

I think his only fault were the parenthesis around spawnPosition[Random.Range(0, 3)]. Try removing those.

I don't see why those would hurt.

Thank you!

@Lockstep : perhaps in unityscript it has no real meaning at all, but in real javascript it does change the semantics. Maybe If I have the time, I'm going to experiment with it. :)