Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Courtneyssg · Feb 21, 2019 at 02:34 AM · coroutinegameobjectsinstantiationarray of gameobjectstimer countdown

Instantiate an array of gameobjects with a time delay between each

I've found some solutions similar to what I am looking for but they don't quite answer my question. The following is the criteria I'm trying to meet:

  • I'm trying to spawn an array of game objects with a time delay between each.

  • The number of items in the array is given by a variable that updates as the number of items in the array changes

  • I'm calling the method in update because the items can't spawn at start, it is only after a given time delay they will start spawning

  • They must stop spawning once every element in the array has spawned

  • They must spawn in order element[0], element[1], etc.

I tried to do something like the following:

 private float countdown;
 private float timeBetweenSpawns;
 private int counter = 0; // this updates depending on the number of items given to the array
 
 void Start()
 {
     countdown = timeBetweenSpawns;
 }
 
 void Update()
 {
     for(int i = 0; i < counter; i++)
     {
         if(countdown <= 0)
         {
             Instantiate(array[i], transform.position, quaternion.identity);
             countdown = timeBetweenSpawns;
         else
         {
             countdown -= time.deltatime;
         }
 }

the main issue with this is that it keeps repeating, and they do seem to spawn out of order at times, I'm not sure why I can't really find a pattern to the mayhem.

I then tried to do something like this:

 for (int i = 0; i < clueCounter; i++)
 {
     StartCoroutine(WaitABit());
     Instantiate(_colors[i], transform.position, Quaternion.identity);
 }
 
 IEnumerator WaitABit()
 {
     yield return new WaitForSeconds(5);
 }

this spawns them in the order they are supposed to be spawned but because I have to call it in update the game objects are constantly spawning. I tried adding in some conditions that will only allow it to spawn once but nothing I'm doing seems to work and it just ends up messing with the order they spawn in.

Anyone have some tips for me to try, this seems like a simple concept but it's giving me some grief.

Comment
Add comment · Show 2
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 RobAnthem · Feb 21, 2019 at 03:17 AM 1
Share

You don't need to loop through, just do this.

 private int spawnedCount = 0;
 void Update()
 {
     if(countdown <= 0)
     {
         if (spawnedCount  +1 < array.Length)
         {
             Instantiate(array[spawnedCount++], transform.position, quaternion.identity);
             countdown = timeBetweenSpawns;
         }
     }
     else
     {
         countdown -= time.deltatime;
     }
 }
avatar image Courtneyssg RobAnthem · Feb 21, 2019 at 11:25 PM 0
Share

many thanks, this did the job!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by carl010010 · Feb 21, 2019 at 04:16 AM

This is off the top of my head but something like this should work

 void Start()
 {
     //Start Spawning right away
     StartCoroutine(WaitABit());
 }
   
 IEnumerator WaitABit()
 {
     //Spawn as many (clues?) as you nee
     for (int i = 0; i < clueCounter; i++)
     {
         Instantiate(_colors[i], transform.position, Quaternion.identity);
         //Wait awhile before spawning new items
         yield return new WaitForSeconds(timeBetweenSpawns);
     }
 }


For a better explanation then I can give I would take a look at the unity coroutines tutorial https://unity3d.com/learn/tutorials/topics/scripting/coroutines?playlist=17117

Comment
Add comment · Show 2 · 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 Courtneyssg · Feb 21, 2019 at 09:01 PM 0
Share

This would occur after a delay from start correct? However I need to be able this sequence to stop and then start up again mid-game. To my understanding, if it's called in start like this once it executes it's done and you won't be able to call it again after the game is running.

avatar image xxmariofer Courtneyssg · Feb 21, 2019 at 09:05 PM 0
Share

you can call StartCoroutine(WaitAbit()) whenever you want. and that will delay an amount set by timeBetweenSpawns is exactly what you asked.

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

110 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I instantiate a runtime created object 1 Answer

Vector3.Lerp curving along three destinations 1 Answer

Checking for objects and adding to array 1 Answer

In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 Answers

Coroutine starts but doesn't run? 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