- Home /
Spawn Objects at Different Locations with Tags?
I'm confused on this situation.
I have 8 objects that I want to randomly spawn at different locations to find and collect when my game starts.
I have 10 empty gameobjects as Spawn Points and I want the game to randomly choose 8 of those spawn points to place each of the 8 individual objects I have to collect. I tagged all of the 8 objects as "SpawnPoint".
I am using this page link text as an example but I'm not sure what to really do... Any help would be extremely appreciated!
Answer by dorpeleg · Jul 13, 2013 at 12:22 AM
You need to do the following:
Use FindGameObjectsWithTag to have all of your spwan points in a list of gameobject.
Make a loop that will run 8 times.
In the loop use
var r = Random.Range(0,7)
to get a random number between 0 and 7 (total of 8 possibilities).
Then get the position of the random chosen spwan point, something like:
var position = listofspwanpointas[r].transform.position;
Then just use Instantiate to spwan your objects.
A more complete version of my answer except the range should be 0 to 9, otherwise you would be randomly selecting 8 of 8 values. He wants 10 possibilities
Thank you very much for the great feedback guys! You both answered my question! :D
@Slobdell I might have miscalculated :)
It was really late when I posted...
Answer by Slobdell · Jul 13, 2013 at 12:12 AM
Just put them into an array or list if they aren't already and do
spawnLocation = spawnPostions[Random.Range(0,9)]
Of course then check to be sure the location hasn't already been used
Your answer
![](https://koobas.hobune.stream/wayback/20220613105613im_/https://answers.unity.com/themes/thub/images/avi.jpg)