Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by PoorTree · Aug 19, 2020 at 08:41 PM · instantiatebeginnerlevelienumeratorwhile-loop

Many loops to instantiate different prefabs during a given time

Hello, I'm returning to game development studies using Unity, I believe the answer to my question is something simple, but I tried and researched a lot without progress.

I'm trying to create a process to randomly instantiate prefabs that are in an array. For now there are only three levels, each lasts 10 seconds and has different prefabs to be instantiated.

I was able to do this using the Inumerator, however for each level i need to create a new loop. Here's the script:

     private float _levelOneTime = 10f;
     private float _levelTwoTime = 20f;
     private float _levelThreeTime = 30f;
 
     public  GameObject[] _levelPrefabs;
 
     private Coroutine _spawnerCoroutine;
 
     void Start()
     {
         _spawnerCoroutine = StartCoroutine(Spawner());
     }

// Here where the magic happens, I had to use three loops for three different instances.

     IEnumerator Spawner()
     {
         while (Time.timeSinceLevelLoad <= _levelOneTime)
         {
             int randomPrefab = Random.Range(0, 2);   
             Instantiate(_levelPrefabs[randomPrefab], new Vector3(0, 0, 10), Quaternion.identity);
             yield return new WaitForSeconds(5);
         }
         while (Time.timeSinceLevelLoad > _levelOneTime && Time.timeSinceLevelLoad <= _levelTwoTime)
         {
             int randomPrefab = Random.Range(3, 5);
             Instantiate(_levelPrefabs[randomPrefab], new Vector3(0, 0, 10), Quaternion.identity);
             yield return new WaitForSeconds(5);
         }
         while (Time.timeSinceLevelLoad > _levelTwoTime && Time.timeSinceLevelLoad <= _levelThreeTime)
         {
             int randomPrefab = Random.Range(6, 9);
             Instantiate(_levelPrefabs[randomPrefab], new Vector3(0, 0, 10), Quaternion.identity);
             yield return new WaitForSeconds(5);
         }
     }
 

I know this will make the script very long if i want to create more levels.

I would like to know if there is a way to instantiate prefabs within the time of their respective levels without having to use the while repetition system so often. ||| Thank you for your attention and patience.

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

0 Replies

· Add your reply
  • Sort: 

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

254 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 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 WaitForSeconds after destroying GameObject to load the next Scene? 0 Answers

Error Instantiating Prefab in while look at end of row, Happens after a random amount of rows 0 Answers

I need help with Instantiate,I need help instantiating blocks! 0 Answers

Need Help instantiating a Game Object Prefab. 0 Answers

How to instantiate UI elements centered around an empty object 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