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 Code_Skunk · Oct 13, 2017 at 05:57 PM · unity 5instantiatewaitforsecondsfor-loopinvoke

Wait For Seconds Instantiate Inside For Loop

This scrip is suppose to instantiate some prefabs after waiting for a few seconds, but the scrips just spits all of the prefabs out after only waiting once. What do i need to work on.

  public GameObject collectablePrefab;
  public Vector3 SpawnThinger;
  private int collectablePrefabCount;
 
    void Start ()
 {
       collectablePrefabCount = 3;
         for (int i = 0; i <= collectablePrefabCount; i++)
         {
             Invoke("spawnCollectables", 2f);
         }
  }
 
    void spawnCollectables()
  {
         float x, y, z;
 
         x = Random.Range(-9f/2, 9f/2);
         y = Random.Range(.5f, 1f);
         z = Random.Range(-9f/2, 9f/2);
         SpawnThinger = new Vector3(x, y, z);
         
         
         Instantiate(collectablePrefab, SpawnThinger, Quaternion.identity);
         Debug.Log(x);
     } 
Comment
Add comment · Show 4
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 Code_Skunk · Oct 13, 2017 at 05:58 PM 0
Share

Scripts* Derp palmface*

avatar image tmalhassan Code_Skunk · Oct 13, 2017 at 06:02 PM 0
Share

Are you using C#?

avatar image tmalhassan tmalhassan · Oct 13, 2017 at 06:03 PM 0
Share

And the waiting time is supposed to be 2 seconds between each instantiation?

Show more comments

2 Replies

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

Answer by TSI25 · Oct 13, 2017 at 06:01 PM

The problem is that all of those invokations are started at the same time from that for loop, and so they all end at the same time. you will want to use a coroutine instead of invoke. that will look like this

         IEnumerator Start()
         {
             collectablePrefabCount = 3;
              for (int i = 0; i <= collectablePrefabCount; i++)
             {
                  yield return new WaitForSeconds(2f);
                  spawnCollectables();
             }
         }
Comment
Add comment · Show 5 · 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 kaplica · Oct 13, 2017 at 06:09 PM 0
Share

Good answer but why make start protected?

avatar image TSI25 kaplica · Oct 13, 2017 at 06:14 PM 0
Share

I have a habit of putting protected in front of my function I think that just slipped in there XD leaving it private would be more than reasonable.

the main reason to make it protected would just be to make it inheritable by sub classes

avatar image kaplica TSI25 · Oct 13, 2017 at 06:26 PM 0
Share

$$anonymous$$ake two classes, one which derives from $$anonymous$$onoBehaviour, second which derives from new base class, try having private and protected.

Show more comments
avatar image Code_Skunk · Oct 13, 2017 at 09:45 PM 0
Share

This works the way i need it to, thank you! But for clarification, the for-loop started all of the spawns at the same time? Why exactly did it only the waiting period only occur once in a for-loop that request the waiting 4 times for my understanding. Thanks again by the way! Big smiles !!

avatar image
1

Answer by Bunny83 · Oct 13, 2017 at 07:00 PM

Another way without a coroutine would be this:

 void Start ()
 {
     collectablePrefabCount = 3;
     for (int i = 0; i <= collectablePrefabCount; i++)
     {
         Invoke("spawnCollectables", 2f + 2f * i);
     }
 }

So you basically start the 4 delayed methods all at the same time but with different delays. 2, 4, 6 and 8 seconds. If it's just a few method calls this would work fine. However if you have many things to chain up it's better to use a coroutine.

Comment
Add comment · Show 3 · 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 Code_Skunk · Oct 13, 2017 at 09:53 PM 0
Share

This works as well, but i don't understand why, could you explain to me what you mean by

"2, 4, 6 and 8 seconds."

and

"However if you have many things to chain up it's better to use a coroutine."

by that you mean having a large sample size for my collectible correct? oh and, LOL i said look at that, i spelled collectible wrong. collectable. S$$anonymous$$DH
avatar image Bunny83 Code_Skunk · Oct 14, 2017 at 02:26 PM 1
Share

This for loop:

 collectablePrefabCount = 3;
 for (int i = 0; i <= collectablePrefabCount; i++)
 {
     Invoke("spawnCollectables", 2f + 2f * i);
 }

Is equivalent to:

 Invoke("spawnCollectables", 2f);
 Invoke("spawnCollectables", 4f);
 Invoke("spawnCollectables", 6f);
 Invoke("spawnCollectables", 8f);

So you start 4 delayed Invoke calls at the same time but each with a different delay. The term 2f + 2f * i will be "2" when "i == 0" and will be "4" when "i == 1" and so on.

When your "collectablePrefabCount" is high you would start a seperate invoke call for each one. The invoke call has to be stored and managed by Unity. So if you want to create 100 it wouldn't make much sense to start 100 seperate invoke calls distributed over time. It would make more sense to use a coroutine and just work through them sequencially.

I just wanted to present another way.

avatar image Code_Skunk Bunny83 · Oct 14, 2017 at 05:44 PM 0
Share

Very nicely put, i understand a lot more about how that works, thank you.

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

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

InvokeRepeating affecting parts of script 1 Answer

Instantiate object in for loop with array 2 Answers

Instantiate for loop skips some objects 1 Answer

How can I naming each instantiated object in "for" loop ? 1 Answer

Order of instantiated objects 0 Answers


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