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 Learn2rage · Sep 17, 2015 at 12:12 PM · coroutinefunctionwaitforsecondsarchitecture

General question about functions, cofunctions and wait timers

Hey,

I'm new to coding and so I'm having trouble wrapping my head around some of the concepts. I'm trying to do everything as "lean" as possible although I'm taking more and more shortcuts and my code starts getting out of hand.

I'm currently writing the spawn script for Tower Defence based game. I want to use random generated wave-types (boss, normal, speed etc.) so I built a WaveController function to keep track of it. The first problem I encountered was how to keep the different wavetypes differentiated in a nice way, I just mashed everything into nested ifs - very bad, but I couldn't come up with anything better atm. The second thing was to set spawnintervals so that everything doesn't spawn at once. I figured that I needed to use Coroutines to use WaitForSeconds but that kinda turned my plan upside down as the functions doesn't wait until a Coroutine has been excecuted until it continues with the rest I want to do. Now I have to time everything and it just seems I've painted myself into a corner using coroutines. What is it that I am not getting here? Continuing down this path seems like a really bad idea, please halp! =/

Maybe I'm not making sense at all, if so I'm terribly sorry!

  1. Textboxes are only there to fault search, ignore them

  2. I use many public variables, I started out like this to test other functionality first. Aiming to clean up later.

Code Below:

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
     
     //Need to take arguments from menu about difficulty, settings etc; later!
 
     // The Monster that should be spawned; Should be randomed
     public GameObject monsterPrefab;
     
     // Spawn Delay in seconds
     //private float spawninterval;
 
     //private float waveinterval = 10f;
 
     public int waveNumbers;
 
     private int[] waves= new int[15];
 
     private int waveCount = 1;
 
     private int monsteramount;
 
     private int wavetype;
 
     public string stringToEdit;
     public string stringToEdit1;
     public string stringToEdit2;
     public string stringToEdit3;
     public string stringToEdit4;
 
     //Need to set different wavetypes;
     //1 = normal : monsteramount 15, speedmod 3, interval 3, sizemod 1, healthmod 1
     //2 = mass : monsteramount 30, speedmod 3, interval 1, sizemod 0.5, healthmod 0.5
     //3 = speed  : monsteramount 15, speedmod 5, interval 3, sizemod 1, healthmod 0.7
     //4 = boss  : monsteramount 1, speedmod 3, interval 3, sizemod 2, healthmod 15
 
     
     //Need to improve mobs as spawns continue; tiers? 1,2,3,4,5?
     
 
 
     // Use this for initialization
     void Start () {
         waveNumbers=5;
 
         //Need to random the waves first
         int count = waveNumbers / 5;
         //stringToEdit4 = "outside loop";
         for (int i=0;i<count;i++) {
             //stringToEdit4 = "inside loop";
             waves[0+i*5]=1; //first wave in a serie of 5 is always normal
             int k = 0;
             while (k<3){
                 waves[k+1+i*5]=Random.Range(1,3);
                 k++;
             }
             waves[4+i*5]=4; //last wave in a serie of 5 is always a boss
         }
         //stringToEdit4 = (waves[0]).ToString();
         //need to start spawning
         //stringToEdit = "start wave controller";
 
         StartCoroutine(WaveController ());
 
 
 
     }
 
     IEnumerator WaveController () {
         //controls the waves
         //stringToEdit1 = "inside wavecontroller";
         int monsteramount;
         float speedmod;
         float interval;
         float sizemod;
         float healthmod;
 
 
         foreach (int wave in waves) {
             stringToEdit = wave.ToString();
             if (wave==1){ //if normal
                 monsteramount = 15;
                 stringToEdit3 = monsteramount.ToString();
                 speedmod = 3f;
                 interval = 3f;
                 sizemod = 1f;
                 healthmod = 1f;
 
                 stringToEdit2 = "Round done, spawning normal in 15";
                 yield return new WaitForSeconds(15);
                 StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
                 yield return new WaitForSeconds(40);
             }
             else {
                 if (wave==2){ //if mass
                 monsteramount = 30;
                 speedmod = 3f;
                 interval = 1.5f;
                 sizemod = 0.5f;
                 healthmod = 0.5f;
 
                 stringToEdit2 = "Round done, spawning mass in 15";
                 yield return new WaitForSeconds(15);
                 StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
                 yield return new WaitForSeconds(40);
                 }
                 else {
                     if (wave==3){ //if speed
                         monsteramount = 15;
                         speedmod = 5f;
                         interval = 3f;
                         sizemod = 1f;
                         healthmod = 0.7f;
                         
                         stringToEdit2 = "Round done, spawning speed in 15";
                         yield return new WaitForSeconds(15);
                         StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
                         yield return new WaitForSeconds(40);
                     }
                     else {
                         //if (wave==4){ //if boss
                             monsteramount = 1;
                             speedmod = 3f;
                             interval = 3f;
                             sizemod = 2f;
                             healthmod = 15f;
                             
                             stringToEdit2 = "Round done, spawning boss in 15";
                             yield return new WaitForSeconds(15);
                             StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
                             yield return new WaitForSeconds(40);
                         //}
                     }
                 }
             }
 
 
 
 
         }
 
     }
 
     IEnumerator WaveSpawner (int amount, float speed, float time, float size, float health) {
         int monstercount = 0;
 
         yield return new WaitForSeconds(time);
         stringToEdit3 = "start to spawn";
         for (int i=0;i<amount;i++) {
             stringToEdit4 = "inside spawnloop";
             GameObject newMonster = Instantiate(monsterPrefab, transform.position, Quaternion.identity) as GameObject;
             newMonster.GetComponent<Monster> ().monsterNumber = monstercount;
             newMonster.GetComponent<Monster> ().transform.localScale.Set(size,size,size);
             newMonster.GetComponent<NavMeshAgent> ().speed = speed;
             stringToEdit1 = monstercount.ToString();
             monstercount = monstercount + 1;
             yield return new WaitForSeconds(time);
         }
 
     }
     
 
     void OnGUI() {
         GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
 
         GUI.TextField(new Rect(10, 30, 200, 20), stringToEdit1, 25);
 
         GUI.TextField(new Rect(10, 50, 200, 20), stringToEdit2, 25);
 
         GUI.TextField(new Rect(10, 70, 200, 20), stringToEdit3, 25);
 
         GUI.TextField(new Rect(10, 90, 200, 20), stringToEdit4, 25);
     }
 
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

How to create a delay in a function being called in update? 2 Answers

Need Help with a Null Reference 1 Answer

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers

C# WaitForSeconds isn't waiting for any seconds 2 Answers

SetTrigger animation won't wait for correct time before continuing code 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