- Home /
 
Instantiate 1 at a time at each transform in array
Hello there, I'm making an enemy spawner and since arrays are fairly new to me I'm stuck at one point. I want it to spawn 1 enemy at each transform in the array. And so far it just spawns everything at first transform in the array. What am I doing wrong?
Here's the code:
 var spawnPoint : Transform[];
 var spawns : GameObject[];
 var difficultyMultiplier : float = 1.0;
 var spawnTime : float = 5.0;
 var spawnsPerSpawnPoint : int = 1;
 private var spawnTimer : float = 0.0;
 
 function Update()
 {
     spawnTimer += Time.deltaTime;
     if(spawnTimer >= 5.0)
     {
         SpawnTimer();
     }
 }
 
 function SpawnTimer()
 {
     for (var i = 0; i < spawnsPerSpawnPoint; i++)
     {
         Instantiate(spawns[(Random.Range(0, spawns.Length))], spawnPoint[(Random.Range(0,spawns.Length))].position, Quaternion.identity);
     }
     spawnTimer = 0.0;
 }
 
               I'm probably mistaken but the way I understand my instantiate line is that it should spawn a random gameobject in the array at a random transform in the other array. :s
Thanks you!
Answer by Tomer-Barkan · Oct 31, 2013 at 03:16 PM
I'm confused.
Your for loop starts from 0, and the condition is < 1. This means there's only one iteration...
Your instantiate location is a random transform, instead of methodically instantiating from start to finish of the array.
If you want to spawn per spawnsPerSpawnPoint spawns in each point:
 function SpawnTimer()
 {
     for (var i = 0; i < spawnPoint.Length; i++)
     {
         for (var j = 0; j < spawnsPerSpawnPoint; j++) {
             Instantiate(spawns[(Random.Range(0, spawns.Length))], spawnPoint[i].position, Quaternion.identity);
         }
     }
     spawnTimer = 0.0;
 }
 
              Are you sure that you are actually populating your array of transforms. I assume you are doing so in the inspector since it's not populated in the script but I had to ask.
Your code should be doing what you said "spawn a random gameobject in the array at a random transform in the other array. :s" but this does not match what you said you wanted in the question "I want it to spawn 1 enemy at each transform in the array."
What your code should be doing currently is spawning 1 (spawnsPerSpawnPoint) random object at 1 random position.
if you want to spawn (spawnsPerSpawnPoint) random objects at each point you need a for loop that goes through each element in the TRANSFOR$$anonymous$$ ARRAY then inside that loop create another loop that executes (spawnsPserSpawnPoint) times. Inside THAT loop spawn a random object at the current transform.
$$anonymous$$aybe I should have just written code but I'm in a crappy browser so the posting tools are not reliable.
I'm confused too. Arrays and raycasting confuses me, although I got 10 times better at raycasting in the last 3 days, I didn't use arrays for anything before so the entire "for" line is confusing to me.
Your code only spawns it on the first transform. :S
$$anonymous$$ine works when I fill the GameObject[] with the same number of prefabs in the array as the SpawnPoint[] array. I know I'm doing something wrong just don't know what cause this is hardly the way to do it. xD
Yes I populate the array in inspector. Sorry I just saw the second comment. I guess I didn't phrase my self right. I want to spawn 1 random game object at random transform from the transform array, every, lets say, 5 seconds.
$$anonymous$$y code will spawn 1 per item in spawnPoint[].
Let me explain:
First I run a loop where I starts from 0, and until the number of items in spawnPoint (`spawnPoint.Length`).
Then there is another loop that runs spawnsPerSpawnPoint times.
For each time spawnsPerSpawnPoint runs, it will spawn a random prefab from spawns, in the location of the current spawnPoint entry. Since it iterates ALL spawnPoint entries, you will get a spawn for each and every transform that you put in it.
In your code, it randomly selects a spawn prefab and a spawn point every 5 seconds, and spawns an enemy there, because you're missing the loop that goes over the entire spawnPoint array.
I hope I explained well enough, and anyway I strongly recommend an arrays tutorial:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays
$$anonymous$$y answer got converted to a commentand doesn't really make sense in it's new context but the code written in this answer is exactly what I was describing so all is well :)
Your answer
 
             Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
Instantiate over time? 1 Answer
Multiple random spawns from an array 1 Answer
Instantiate from array into array? 2 Answers
Spawning Objects Using An Array. 1 Answer