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
0
Question by ogike · Feb 28, 2014 at 04:17 PM · spawningenemy spawn

Spawnerscript only spawns 5 enemy

I made a Spawn script, but it only spawns 5 enemys. Here's the script:

 var spawnPoints : Transform[];      // Array of spawn points to be used.
 var enemyPrefabs : GameObject[];     // Array of different Enemies that are used.
 var amountEnemies : float;              // Total number of enemies to spawn.
 var yieldTimeMin = 2;                  // Minimum amount of time before spawning enemies randomly.
 var yieldTimeMax = 5;                  // Don't exceed this amount of time between spawning enemies randomly.
 var enemiesToSpawnAtTheSameTime : int = 1;
 var StartTime = 5;
 var WaveWait = 5;
 var WaveText : GUIText;
 var WaveCount : int;
 var amountEnemiesPlus = 5;
 var IsSpawning : boolean;
 private var wait : boolean = false;
 
 function Start()
 {
     
     yield WaitForSeconds(StartTime);
     SpawnWave();
     WaweCount = 1;
     amountEnemies = 15;
 }
 
 function Update()
 {
     if(IsSpawning == false)
      {
       StartWave();
      }
 }
 
 function StartWave()
 {
     yield WaitForSeconds(WaveWait);
     SpawnWave();
 }
 
 function SpawnWave()
 { 
     if(wait) return;
     
     if(amountEnemies > 0)
       {
         IsSpawning = true;
         wait = true;
         yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
         for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++){
             var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
             var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.
         
             Instantiate(obj, pos.position, pos.rotation);
         }
         amountEnemies --;    
         wait = false;
         if(amountEnemies == 0)
          {
           WaveCount += 1;
           IsSpawning = false;
           amountEnemies += amountEnemiesPlus;          
          }
       }   
               
 }  

There are no errors. Any help would be appreciated.

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 ogike · Mar 03, 2014 at 01:46 PM 0
Share

Please somebody help me.

avatar image ogike · Aug 16, 2015 at 06:48 PM 0
Share

I will put a ---> behind the parts that i would have needed to change:

 var spawnPoints : Transform[];      // Array of spawn points to be used.
  var enemyPrefabs : GameObject[];     // Array of different Enemies that are used.
  var amountEnemies : float;              // Total number of enemies to spawn.
  var yieldTime$$anonymous$$in = 2;                  // $$anonymous$$inimum amount of time before spawning enemies randomly.
  var yieldTime$$anonymous$$ax = 5;                  // Don't exceed this amount of time between spawning enemies randomly.
  var enemiesToSpawnAtTheSameTime : int = 1;
  var StartTime = 5;
  var WaveWait = 5;
  var WaveText : GUIText;
  var WaveCount : int;
  var amountEnemiesPlus = 5;
  var IsSpawning : boolean;
 /*--->*/ private var BetweenWaves : boolean;
  private var wait : boolean = false;
  
  function Start()
  { 
      yield WaitForSeconds(StartTime);
    --//SpawnWave();
    | WaweCount = 1;
    | amountEnemies = 7;
    ->SpawnWave();
  }
  
  function Update()
  {
      //if(IsSpawning == false)
      /*--->*/if(IsSpawning == false && WaveCount > 0 && !BetweenWaves)
       {
        /*--->*/vBetweenWaves = true;
        StartWave();
       }
  }
  
  function StartWave()
  {
      yield WaitForSeconds(WaveWait);
      /*--->*/amountEnemies += amountEnemiesPlus;  
      SpawnWave();
      /*--->*/BetweenWaves = false;
  }
  
  function SpawnWave()
  { 
      if(wait) return;
      
      //if((amountEnemies > 0))
      /*--->*/while(amountEnemies > 0)
        {
          IsSpawning = true;
          wait = true;
          yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));  // How long to wait before another enemy is instantiated.
          for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++){
              var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
              var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.
          
              Instantiate(obj, pos.position, pos.rotation);
          }
          amountEnemies --;    
          wait = false;
          if(amountEnemies == 0)
           {
            WaveCount += 1;
            IsSpawning = false; 
            //amountEnemies += amountEnemiesPlus;       
           }
        }   
                
  }  









2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Christian.Tucker · Feb 28, 2014 at 04:46 PM

I don't know what you mean, is it supposed to continuously spawn? If so start a CoRoutine or at the end of SpawnEnemies do something like

 float waitTime = 5.0f; // 5 seconds
 Invoke("SpawnWave", waitTime);
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 ogike · Mar 01, 2014 at 04:29 PM 0
Share

It doesn't working with Invoke. How can I do it with InvokeRepeating that if there's no more enemies in the wave it will stop for a few $$anonymous$$utes and add more enemies?

avatar image
0

Answer by Dblfstr · Mar 03, 2014 at 01:58 PM

I looks like isSpawning will never be set to false again. In your update function, you check is isSpawning is false. And if it is false, then you run the spawn function. This will spawn your enemies. Then you immediately check if enemies==0; After your first iteration, amountEnimies should be 14 (starts at 15, then decrements by one. So amountEnimies == 0 will be false. Now the spawn function stops. However amountEnemies has to 0 before isSpawning is set bacl to false, I don't think this will ever happen. So in your update, you will only call startWave() once, isSpawning is set to true then and never reset. I think.

     var spawnPoints : Transform[]; // Array of spawn points to be used.
     var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
     var amountEnemies : float; // Total number of enemies to spawn.
     var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
     var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
     var enemiesToSpawnAtTheSameTime : int = 1;
     var StartTime = 5;
     var WaveWait = 5;
     var WaveText : GUIText;
     var WaveCount : int;
     var amountEnemiesPlus = 5;
     var IsSpawning : boolean;
     private var wait : boolean = false;
      
     function Start()
     {
         yield WaitForSeconds(StartTime);
         SpawnWave();
         WaweCount = 1;
         amountEnemies = 15;
     }
      
     function Update()
     {
         if(IsSpawning == false)
         {
             StartWave();
         }
     }
      
     function StartWave()
     {
        yield WaitForSeconds(WaveWait);
         SpawnWave();
     }
      
     function SpawnWave()
     {
         if(wait) return;
 if(amountEnemies > 0)
         {
             IsSpawning = true;
             wait = true;
          yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
             for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++)
 {
                 var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
 var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; 
                      Instantiate(obj, pos.position, pos.rotation);
             }
             amountEnemies --;
             wait = false;
             
         }
     //Moved this part out of your: if(amountEnemies >0)
 if(amountEnemies == 0)
         {
             WaveCount += 1;
             IsSpawning = false;
             amountEnemies += amountEnemiesPlus;
         }
 
      }
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 ogike · Mar 04, 2014 at 04:15 PM 0
Share

If I disable the is spawning after it spawned the 5 enemies it will do it again and if I do it again until the amount of enemies is 0 it's working again. Or if i set the amount of enemies to 5 by default it's working.

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Having camera attack upon spawning player? 1 Answer

enemy spawning when dead 1 Answer

How to import the object from server to unity 2 Answers

Need help with easy scripting 2 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