• 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
Question by Erriikk · May 09, 2019 at 01:50 PM · unity 2dspawnspawningspawning problemsspawning-enemies

How to increase the spawn rate of an object over time?

Hey everyone, I'm new to coding. It seems like people are very nice and helpful on here, so I thought I'd ask a question I've been stuck on for a long time now. I'm currently making a 2D game where balls are dropping from the top of the screen to the bottom of the screen. Your goal is to pick them up, and if you let one fall on the ground, you lose. The spawn rate is set to 2 sec. I want t$$anonymous$$s increased by 0.1 sec every 30 seconds (2 sec, 1.9 sec, 1.8 sec, etc.). I feel like I'm close, but I can't seem to get it to work. Here is the code I'm working on. Tell me, how can I make t$$anonymous$$s happen? Thank you in advance!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnBallsNew : MonoBehaviour
 {
     public GameObject fallingObject;
 
     float delayAndSpawnRate = 2;
     float timeUntilSpawnRateIncrease = 30;
 
     void Start()
     {
         InvokeRepeating("SpawnObject", delayAndSpawnRate, delayAndSpawnRate);
         StartCoroutine(ScheduleIncreases());
     }
 
     void SpawnObject()
     {
         float x = Random.Range(-11, 11);
 
         Instantiate(fallingObject, new Vector2(x, 7), Quaternion.identity);
     }
 
     IEnumerator ScheduleIncreases()
     {
         yield return new WaitForSeconds(timeUntilSpawnRateIncrease);
         IncreaseSpawnRate();
         StartCoroutine(ScheduleIncreases());
     }
 
     void IncreaseSpawnRate()
     {
         if (delayAndSpawnRate > 1)
         {
             delayAndSpawnRate -= 0.1f;
         }
     }
 }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by Hellium · May 09, 2019 at 02:08 PM

 public class SpawnBallsNew : MonoBehaviour
 {
     public GameObject fallingObject;
 
     float delayAndSpawnRate = 2;
     float timeUntilSpawnRateIncrease = 30;
 
     void Start()
     {
         StartCoroutine(SpawnObject(delayAndSpawnRate));
     }
 
     IEnumerator SpawnObject( float firstDelay )
     {
         float spawnRateCountdown = timeUntilSpawnRateIncrease ;
         float spawnCountdown = firstDelay ;
         w$$anonymous$$le( true )
         {
             yield return null ;
             spawnRateCountdown -= Time.deltaTime;
             spawnCountdown     -= Time.deltaTime;
 
             // Should a new object be spawned?
             if( spawnCountdown < 0 )
             {
                 spawnCountdown += delayAndSpawnRate;
                 Instantiate(fallingObject, new Vector2(Random.Range(-11, 11), 7), Quaternion.identity);
             }
 
             // Should the spawn rate increase?
             if( spawnRateCountdown < 0 && delayAndSpawnRate > 1 )
             {
                 spawnRateCountdown += timeUntilSpawnRateIncrease;
                 delayAndSpawnRate -= 0.1f;
             }
         }
     }
 }
Comment
Erriikk
rustybeast

People who like this

2 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 Erriikk · May 09, 2019 at 03:17 PM 0
Share

Thank you for such a quick response, I really appreciate it. I did not manage to get it to work however, it got a bit confusing when you set variables I've already defined to mean something new, and I also didn't understand whether or not what you answered was the complete script or something I should add into mine (I assumed the complete script though). I edited the question text, as I might have been unclear earlier, so just in case, take a look again if you can. What should I do? @Hellium

avatar image Hellium Erriikk · May 09, 2019 at 03:26 PM 0
Share

The script is meant to replace yours.

What does not work with it?

spawnRateCountdown is the remaining time before the delayAndSpawnRate is decreased.

spawnCountdown is the remaining time before a new falling object is instantiated.

When those countdown reach 0, they are reset to their original values (`timeUntilSpawnRateIncrease` and delayAndSpawnRate respectively)

avatar image Erriikk Hellium · May 09, 2019 at 05:25 PM 0
Share

Lol, not sure what I was doing the first time around while testing, as I tested it again and it worked perfectly. I guess I just got a bit confused from the code, since I'm new to this and, yeah, anyways, it worked perfectly, so thank you very much for helping me so quickly, have a great rest of your day/night.

avatar image

Answer by ajmracer032706 · Sep 29, 2020 at 10:58 AM

@Hellium you may not see t$$anonymous$$s but it is worth a shot anyway. I am currently using your code and I have the Delay and Spawn Rate set to 20 along with the timeUntilSpawnRateIncrease set to 1. For some reason the script stops working when the Delay and Spawn Rate reaches 18.09999. Any suggestions?,I don't know if anyone will see t$$anonymous$$s but when I implement t$$anonymous$$s code it stops working after some of the countdown. If I start it at 20 Delay and Spawn Rate with timeUntilSpawnRateIncrease of one it ends at 18.09999999. If anyone sees t$$anonymous$$s that may have a fix it is very apprecitated!

Comment

People who like this

0 Show 0 · 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

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

113 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 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 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 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 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

Spawning help 1 Answer

How to spawn all humans at once and activate then with time? 1 Answer

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers

SPAWNING ENEMIES and controlling the amount/interval 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