- Home /
Multiple spawn points without spawning in the same place.
I'm trying to spawn some GameObjects to random spawn points, but they are spawning in the same spawnpoint, how can I make then spawn in different spawnpoints?
I'm using C#. Here is the code:
public Transform[] spawns; public GameObject Bone; public int amountThings = 2; public int i = 0;
public void Start() { Spawn(); }
public void Spawn() { for (i = 0; i < amountThings; i++) {
Transform pos = spawns[Random.Range(0, spawns.Length)];
Instantiate(Bone, pos.position, pos.rotation);
}
}
Sorry about my English.
Answer by Bunny83 · May 17, 2011 at 11:28 PM
Well, if you just want to make sure during a spawn event (where you spawn multiple objects at the same time) that every object gets a unique spawn point you can use a copy of your list and remove the used spawn points. That would be the easiest way. If you want to spawn continuously new objects and some of the used spawn points are free again, you have to use either some sort of trigger around each spawnpoint to tell if it's "free", or if you're sure that the point is free after e.g. 5 seconds just give them a timeout.
note: no syntax check performed ;) You will also need to add the System.Collections.Generic;
namespace.
public Transform[] spawns; public GameObject Bone; public int amountThings = 2; public int i = 0;
public void Start() { Spawn(); }
public void Spawn() { List<Transform> freeSpawnPoints = new List<Transform>(spawns); for (i = 0; i < amountThings; i++) { if (freeSpawnPoints.Count <=0) return; // Not enough spawn points int index = Random.Range(0, freeSpawnPoints.Count); Transform pos = freeSpawnPoints[index]; freeSpawnPoints.RemoveAt(index); // remove the spawnpoint from our temporary list Instantiate(Bone, pos.position, pos.rotation); } }
I'm trying to use this code but have an error here:
System.Collections.Generic.List ' does not contain a definition for Length' and no extension method
Length' of type `System.Collections.Generic.List ' could be found.
I'm using System.Collections.Generic
I changed Length for Count and now it is ok! Thank you! =D
Sorry :D my fault, i just changed the name from your code. You're right it's Count
. I will change my answer ;)
Answer by Antony-Blackett · May 17, 2011 at 11:05 PM
I assume you mean they are in the same place each time you run your game?
If so the the trouble you're having is that the Random seed is the same every time you run your game. This isn't really a bad thing, it means that you can easily reproduce and debug issues that you find resulting from random numbers. But if you want your game to be random every time you play you'll have to use something like your systems clock to set the seed on the Random class.
try adding a line to your start function like this:
void Start()
{
Random.seed = System.DateTime.Now.Second;
Spawn();
}
I don't think he had this in $$anonymous$$d ;). I guess that his issue is that the script spawns sometimes two or more objects at the same spawnpoint because Random.Range returned the same number again.
Bunny83 answered correctly. The objects in the same spawnpoint is because they returned the same number.
Your answer
Follow this Question
Related Questions
how to freeze a random position? 1 Answer
Cards To Hand 0 Answers
Two different random numbers 2 Answers
How to join a specific image to random button...?,Hello everybody, 0 Answers
Randomly instantiating a list of objects at a random position. 1 Answer