Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ilkzz · Apr 15, 2014 at 08:27 PM · javascriptspawnwaves

Spawning enemies in waves

Hi,

I am trying to make a spawn system in waves. My spawn is working but the wave section is not.

Once one enemy dies, enemies are just randomly spawn out of control

 var location:Transform;
 var enemySelect:Rigidbody2D;
 var spawn1:Transform;
 var spawn2:Transform;
 var spawn3:Transform;
 var waveTime:float;
 var waveLevel:int = 1;
 var totalEnemy:int = 2;
 var enemyAlive:int = 0;
 var waitToWave:float = 10;
 var waveMultiplier:int = 2;
 var enemy1:Rigidbody2D;
 var enemy2:Rigidbody2D;
 var enemy3:Rigidbody2D;
 
 
 function Awake () 
 {
     Spawn();
     //enemyCount = GameObject.FindGameObjectsWithTag("Enemy");
 //var tempobj : GameObject = GameObject.FindWithTag("Enemy");
 var countScript : EnemyHealth = GetComponent(EnemyHealth);
 
 }
 
             
 
 
 
 function FixedUpdate () {
 
 
       
      
     if (enemyAlive == 0) { 
         //the is no creature left; create a wave!
         waveLevel++;
         totalEnemy = totalEnemy * waveMultiplier;
         Spawn();
         }
         
         if (EnemyHealth.health == 0){
         enemyAlive--;
        
      Debug.Log("enemy died");
      }
      }
      
         
     
     
     
     if (waitToWave <= waveTime) {
         waveTime = 0; 
         //reset time
         //enemyAlive = totalEnemy;
         
      }
      
      
 
     
     
             function Spawn() 
     {
   
      for(var i:int=0; i<totalEnemy; i++) {
             enemyAlive++;
      //select a random number, inside a maths function absolute command to ensure it is a whole number
      var randomPick:int = Mathf.Abs(Random.Range(1,3));
      
      
      
      //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
      var location1:Transform;
      location1 = spawn1;
      
      var location2:Transform;
      location2 = spawn2;
      
      var location3:Transform;
      location3 = spawn3;
      
      if(randomPick == 1){
       location = location1;
       Debug.Log("Choose loc 1");
      }
      else if(randomPick == 2){
       location = location2;
       Debug.Log("Choose loc 2");
      }
      else if(randomPick == 3){
       location = location3;
       Debug.Log("Choose loc 3");
      }
      //select a random number, inside a maths function absolute command to ensure it is a whole number
      var randomPickEnemy:int = Mathf.Abs(Random.Range(1,4));
      
 
      //check what randomPick is, and select one of the 3 locations, based on that number
      if(randomPickEnemy == 1){
       enemySelect = enemy1;
       Debug.Log("Choose enemy 1");
      }
      else if(randomPickEnemy == 2){
       enemySelect = enemy2;
       Debug.Log("Choose enemy 2");
      }
      else if(randomPickEnemy == 3){
       enemySelect = enemy3;
       Debug.Log("Choose enemy 3");
      }
      
      
      
      
      //create the object at point of the location variable
      var thingToMake:Rigidbody2D;
      Debug.Log(location);
      thingToMake = Instantiate(enemySelect, location.position, location.rotation);
      thingToMake.AddForce(Vector3(0,0,100));
      
      
      //halt script for 1 second before returning to the start of the process
      yield WaitForSeconds(5);
 
     }
     }

I have tried for hours trying to move loops around,brackets etc. but all has failed

what am i doing wrong?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LeGrandBlond · Apr 15, 2014 at 08:57 PM

I am afraid you are doing it in the wrong way. Calling WaitForSeconds() inside Spawn() which is called inside FixedUpdate() is incorrect. You need a co-routine. I suggest to have a look at the code here : http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves Feel free to watch the video, it is an absolutely brilliant tutorial explaining how to spawn enemy waves!

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 akauper · Apr 15, 2014 at 09:26 PM 0
Share

You can also use InvokeRepeating

avatar image Ilkzz · Apr 15, 2014 at 09:34 PM 0
Share

i tried to play the video it has no sounds and why do you say i am doing it all wrong why exactly is it wrong ? it does work the only issue is when enemy is killed and the enemyAlive count drops to 0 it continuously spawns players when i look at debug it reports that enemy has died several times even doe it has not

avatar image akauper · Apr 15, 2014 at 09:40 PM 0
Share

It seems you want your code "fixed" here, rather than trying to learn what is causing it to fail.

As LeGrandBlond said, you approach to the problem is incorrect. Calling WaitForSeconds(5) at the end of your spawn function doesnt accomplish anything, it simply tells unity to wait 5 seconds before cleaning up Spawn function's information from memory.

It is not that case that the spawn function can only be run when another spawn function finishes. Your script is continuously running the spawn function and each of them is waiting for 5 seconds at the end(acomplishing nothing). What you need is a Coroutine to halt the CALLING of the spawn function.

However, I have found that InvokeRepeating can be easier to understand at first. Take a look at the Unity Documentation for InvokeRepeating and StartCoroutine

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

22 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

Related Questions

How to spawn objects in a specific range of random location 1 Answer

instantiate a set amount 3 Answers

Randomize Spawn Times 0 Answers

Semi - Advanced Spawn System 1 Answer

Spawn game object in random position on screen 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