Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChrisSch · Oct 31, 2013 at 03:08 PM · instantiatearraytimespawnarrays

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Oct 31, 2013 at 03:16 PM

I'm confused.

  1. Your for loop starts from 0, and the condition is < 1. This means there's only one iteration...

  2. 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;
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheValar · Oct 31, 2013 at 03:23 PM 1
Share

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.

avatar image ChrisSch · Oct 31, 2013 at 03:27 PM 0
Share

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

avatar image ChrisSch · Oct 31, 2013 at 03:30 PM 0
Share

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.

avatar image Tomer-Barkan · Oct 31, 2013 at 03:32 PM 0
Share

$$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

avatar image TheValar · Oct 31, 2013 at 03:59 PM 1
Share

$$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 :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges