- Home /
Question by
wisekevin · Jun 02, 2014 at 08:02 PM ·
transform.
2D Spawn with fixed random locations
Hey, I'm trying to make a block spawn in a one of 5 places randomly. It's in 2D and I thought of using an array, but couldn't work out adding the locations to the array. Here is the code I have so far: #pragma strict
public var spawn: int;
public var blockprefab : Rigidbody2D;
public var Spawnpoints: Transform[] = new Transform[5];
public var countdown : float;
function Update ()
{
spawn = Random.Range(1.0, 5.0);
countdown -= Time.deltaTime;
var SpawnpointsNumber : int = spawn;
var Spawnpoints : Transform = Spawnpoints[SpawnpointsNumber];
if(countdown <= 0.0f)
{
var blockprefabInstance : Rigidbody2D;
blockprefabInstance = Instantiate(blockprefab,Spawnpoints.position, Spawnpoints.rotation) ;
countdown = Random.Range(1.0, 5.0);
}
}
How do I make it so one of 5 random spawnpoints is chosen and one is used?
Feel free to edit and play around with the code! Any help is much appreciated!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613150619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Ai Prefab Spawning in Without a target? 1 Answer
Finding the minimum distance between randomly spawned objects in a 9 x 9 grid. 1 Answer
How to stop multiple overlapping spawns at one specific position? 1 Answer
My character isn't moving 0 Answers
How do I use a function in a class more than once? 4 Answers