- Home /
Spawn a prefab at different objects
How do I get it so if I have multiple empty objects around with the tag "Spawner" how to I get a prefab to spawn at two random spawner tagged objects every certain amount of seconds
Answer by Esa · Feb 17, 2012 at 06:20 AM
You could give those spawn locations an integer ID. Then select the wanted amount of random numbers with Random.Range() and then just For-loop the list of your spawn locations. When the random number matches the spawn locations ID you spawn one there.
Answer by Lord Moon · Feb 17, 2012 at 10:00 PM
@Esa ... I did that I made this:
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 Update () {
}
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);
}
}
But when I run the game I get this error: NullReferenceExpetion: Object reference not set to an instance of an object Spawnrandom.Start() (at Assets/Scriptadoodles/Spawnrandom.js 56) ...Yeah my script folder name is goofy =/
Your answer
Follow this Question
Related Questions
Random Spawn, Random Prefab 2 Answers
c# Enemy's spawning over time, problem 2 Answers
How to Make an Expanding Spawn Area for Prefabs? 2 Answers
Why are my bullets shooting at an unpredictable rate? 3 Answers