• 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 Waffle Dragon · Mar 03, 2014 at 12:54 PM · c#enemytimespawnspawner

c# Enemy's spawning over time, problem

I'm trying to get infinite enemy's to spawn more frequently over time. atm the "numberOfEnemy" (3) will spawn but no more after that. what am i doing wrong or if there is a better way to do this that would be great. :)

     public GameObject enemy;
 
     private float timePassed = 0f;
     private float spawnRate = 0f;
     private int numberOfEnemy = 3;
 
     private int m = 3;
     private int i = 0;
 
     // Update is called once per frame
     void Update () {
 
         timePassed += Time.deltaTime;
         spawnRate += Time.deltaTime;
 
         /*if (timePassed >= 60f) {            
             
             if (m >= 2) {
                 m -= 1;
             }
 
             numberOfEnemy += 1;
             timePassed = 0f;
         }*/
 
         if (spawnRate >= m) {
             while (i < numberOfEnemy) {
                 Instantiate(enemy);
                 i++;
             }
             spawnRate = 0f;
         }
     }
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

2 Replies

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

Answer by Waffle Dragon · Mar 03, 2014 at 01:13 PM

I managed to solve this one. Ill post it for anyone else looking for a similar fix. Although if you have a better method please post below.

     public GameObject enemy;
 
     private float timePassed = 0f;
     private float spawnRate = 0f;
     private int numberOfEnemy = 2;
     private float m = 4f;
 
     void Awake () {
         spawnRate = Time.time + m;
     }
 
     // Update is called once per frame
     void Update () {
 
         timePassed += Time.deltaTime;
 
         if (timePassed >= 10f) {            
             if (m >= 1) {
                 m -= 0.5f;    
             }
             numberOfEnemy += 1;
             timePassed = 0f;
         }
 
         if (spawnRate < Time.time) {
             for (int i = 0; i < numberOfEnemy; i++) {
                 Instantiate(enemy);
             }
             spawnRate = Time.time + m;
         }
     }
 }
 
Comment
Add comment · 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
0

Answer by tw1st3d · Mar 03, 2014 at 01:24 PM

If I had to pick a way to do it, I'd do it like this.

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehavior
 {
     public GsameObject enemy;
     private float timePassed = 0f, spawnRate = 0f, m = 4f, spawnTimer = 0f;
     private int numberOfEnemy = 2;
     
     void Start()
     {
         StartCoroutine(Speed());
         StartCoroutine(Spawn());
     }
     
     IEnumerable Speed()
     {
         timePassed += Time.deltaTime;
         if(timePassed >= 10.1f) {
             timePassed = 0.0f;
             numberOfEnemy++;
             spawnRate += 1.0f;
         }
     }
     
     IEnumerable Spawn()
     {
         spawnTimer += Time.deltaTime;
         if(spawnTimer > spawnRate) {
             for(int i = 0; i < numberOfEnemy; i++) {
                 Instantiate(this.enemy);
             }
             spawnTimer = 0.0f;
         }
     }
 }
Comment
Add comment · Show 2 · 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 Waffle Dragon · Mar 04, 2014 at 03:02 AM 0
Share

I like your method but when i try to use it I get an error 'Spawn.Speed()': not all code paths return a value

avatar image Waffle Dragon · Mar 04, 2014 at 03:32 AM 0
Share

i added yield return for all the variables in spawn() and speed(), but only the first 2 enemy's spawn and if spawnRate is higher then 0 they don't spawn at all?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Wave Spawner not working |Does not detect if an object is destroyed 1 Answer

Spawn random Obstacles along the x-axis 0 Answers

More Efficient Enemy Spawning Than This? 2 Answers

random enemy spawn from day night cycle 2 Answers

Enemy spawning problem... 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