Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by Aseyler · Jul 12, 2015 at 06:36 PM · c#listrandomspawnwave

Random element from the list

Hi! I have simple wave spawn system in which the waves(elements in the list) are successively. But I need a random sequence of waves from the list and I don't know how do it.

It's maybe look like

 waves [Random.Range(0, waves.Count)];

Please help!

Thanks in advance!

alt text

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class WaveAction
 {
     public string name;
     public float delay;
     public GameObject[] hazards;
     public int spawnCount;
     public string message;
     public float spawnWait;
     public Vector3 spawnValues;
 
 }
 
 [System.Serializable]
 public class Wave
 {
     public string name;
     public List<WaveAction> actions;
 
 }
 
 
 
 public class WaveGenerator : MonoBehaviour
 {
     public float difficultyFactor = 0.9f;
     public List<Wave> waves;
     private Wave m_CurrentWave;
     public Wave CurrentWave { get {return m_CurrentWave;} }
     private float m_DelayFactor = 1.0f;
 
     void Start()
     {
         StartCoroutine(SpawnLoop());
     }
 
     IEnumerator SpawnLoop()
     {
         m_DelayFactor = 1.0f;
         while(true)
         {
             foreach(Wave W in waves)
             {
                 m_CurrentWave = W;
                 foreach(WaveAction A in W.actions)
                 
                 {
                     if(A.delay > 0)
                         yield return new WaitForSeconds(A.delay * m_DelayFactor);
                     if (A.message != "")
                     {
                         //messageText.text = A.message;
                     }
                     if (A.hazards != null && A.spawnCount > 0)
                     {
                         for(int i = 0; i < A.spawnCount; i++)
                         {
                             GameObject hazard = A.hazards [Random.Range (0, A.hazards.Length)];
                             Vector3 spawnPosition = new Vector3 (Random.Range (-A.spawnValues.x, A.spawnValues.x), A.spawnValues.y, A.spawnValues.z);
                             Quaternion spawnRotation = Quaternion.identity;
                             Instantiate (hazard, spawnPosition, spawnRotation);
                             yield return new WaitForSeconds (A.spawnWait);
                         }
                     }
                 }
                 yield return null;  // prevents crash if all delays are 0
             }
             m_DelayFactor *= difficultyFactor;
             yield return null;  // prevents crash if all delays are 0
         }
     }    
 }


unity-2015-07-12-20-08-00-65.jpg (26.4 kB)
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 iamsidv · Jul 13, 2015 at 08:55 AM 0
Share

can you elaborate more on "But I need a random sequence of waves from the list"

Is it like you need the waves for example in this sequence say.... 2..1..0..3..2..3..4..5 etc? These numbers are index btw

avatar image Aseyler · Jul 13, 2015 at 09:39 AM 0
Share

I need to the waves to go in a random sequence, like your example.

1 Reply

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

Answer by Bunny83 · Jul 13, 2015 at 10:00 AM

Well, i designed this spawning system so you can setup different waves which usually get more difficult later. (Think about WarCraft3 tower defence maps. They often have 100 waves with increasing difficulty). Choosing waves at random in such a case usually doesn't make sense. However you can of course implement a random order.

If you don't care that it might pick the same wave twice in a row the line you posted in your question is already enough to pick a random wave.

If it should be an endless game just remove the foreach loop:

  IEnumerator SpawnLoop()
  {
      m_DelayFactor = 1.0f;
      while(true)
      {
          m_CurrentWave = waves [Random.Range(0, waves.Count)];
          foreach(WaveAction A in m_CurrentWave.actions)
          // ...

If you want to first use up all waves before one repeats you would use a temporary List, fill it with all available waves, pick a random one and remove that wave from the temporary list. If the list is empty (all waves have been used) you have to re add all waves from the original list.

If in addition you want to prevent that the same wave is picked twice in a row when this re-adding occurs, you can use 3 temporary lists. You would use one active list (from which you pick your wave) one "used" list and a "buffer" list. When you pick a wave from the active list you remove it from the active list and add it to the buffer list. If the buffer list has more than "5" waves in it, you move the first item from the "buffer" to the "used" list. When the active list is empty you refill it with the waves in the used list. That way it's not possible to pick one of the last 5 waves since they are still in the buffer. Those waves will be used in the next cycle. I just picked "5" in case there are about 50 waves. So about 10% of the available waves. If you only have 7 waves it makes no sense to buffer 5.

Comment
Add comment · Show 1 · 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 Aseyler · Jul 13, 2015 at 10:40 AM 0
Share

Thank you very much for the answer and for your spawn system! Random sequence seemed more suitable for my game because it must be endless Your sample code is what I need. But I also try to use your 3 temporary lists system. Thanks again!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to fix some of location have more than 1 object? 2 Answers

Instantiate prefab at certain positions 3 Answers

Spawn various enemies at random 1 Answer

AI spawning areas. What are the ways to make them? 1 Answer

How to Spawn an Extra Platform for the Player to Jump On If There Are None Within the Player's Field of View? 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