• 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 /
  • Help Room /
avatar image
0
Question by ShinJohnSnyder · Mar 22, 2022 at 06:31 PM · instantiatespawningtime.deltatimespawning problemsfloats

How to spawn multiple prefabs at different spawn rates

I consider myself to be pretty beginner level when it comes to Unity and programing in C#. In the project I'm working on my goal for t$$anonymous$$s script is to have it be able to spawn different type of enemies at different times for each spawn point. Basically to create a single script for all spawn points instead of having to create a separate script for each enemy spawn point. T$$anonymous$$s is what I have so far.

 public class EnemyWaveSpawner : MonoBehaviour
 {
     public GameObject[] enemiesToSpawn;
 
     //public float spawnRate;
     //public float spawnStart;
     private float waitToSpawn;
     //public int[] waveStartNumber;
     private int currentWaveNumber;
     //private int waveCounter = 3;
     public WaveManager waves;
 
 
     // Start is called before the first frame update
     void Start()
     {
         waitToSpawn = 0f;
     }
 
     // Update is called once per frame
     void Update()
     {
         //SpawnManager();
         SpawnEnemy(waves.waveStartNumber[0], waves.enemySpawnNumber[0], waves.spawnRates[0]);
 
         currentWaveNumber = KaijuLevelManager.instance.waveNumber;
        
         waitToSpawn -= Time.deltaTime; 
         if (waitToSpawn <= 0)
         {
             waitToSpawn = 0;
         }
     }
 
     /*private void SpawnManager()
     {
         switch (waveCounter)
         {
             case 3:
                 SpawnEnemy(waves.waveStartNumber[0], waves.enemySpawnNumber[0], waves.spawnRates[0]);
                 break;
         }
     }*/
 
     private void SpawnEnemy(int waveStart, int t$$anonymous$$sSpawn, float spawnRate)
     {
         if (KaijuLevelManager.instance.enemiesActive && currentWaveNumber >= waveStart)
         {
             if (waitToSpawn <= 0)
             {
                 GameObject enemyClone = Instantiate(enemiesToSpawn[t$$anonymous$$sSpawn], transform.position, transform.rotation);
                 waitToSpawn = spawnRate;
             }
         }
     }
 }

T$$anonymous$$s script is still a work of progress. I figured out how to set it up so I can spawn different enemies of my choice and for each start at different waves. The problem I have is that I can't really figure out how to spawn them at different times. As you could probably tell in my script there's a single spawn timer counting down. I can already see how t$$anonymous$$s wont work for different spawn rates for each enemy. My first solution was to try put "waitToSpawn" local to the the function instead of the script like t$$anonymous$$s.

 private void SpawnEnemy(int waveStart, int t$$anonymous$$sSpawn, float spawnRate)
     {
         float t$$anonymous$$sWaitToSpawn = spawnRate;
         if (KaijuLevelManager.instance.enemiesActive && currentWaveNumber >= waveStart)
         {
             t$$anonymous$$sWaitToSpawn -= Time.deltaTime;
             if (t$$anonymous$$sWaitToSpawn <= 0)
             {
                 GameObject enemyClone = Instantiate(enemiesToSpawn[t$$anonymous$$sSpawn], transform.position, transform.rotation);
                 t$$anonymous$$sWaitToSpawn = spawnRate;
             }
         }
     }

The problem I run into when trying to do t$$anonymous$$s is that "t$$anonymous$$sWaitToSpawn" will not reset itself to equal "spawnRate". If I try to put "t$$anonymous$$sWaitToSpawn" at zero from the beginning then it'll continuously spawn the object non stop(it will eventually stop when the level manager stops its countdown and turn the bool, enemiesActive, false). At t$$anonymous$$s point I'm pretty much banging my head on the wall trying to figure t$$anonymous$$s out. Do you have any possible suggestions on how to precede forward?

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.

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

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

Spawn system that doesn't instantiated enemies on top of player or each other (C#) 2 Answers

Make a gameobject spawn before enemies spawn? (C#) 1 Answer

Floats not being added to other floats? 1 Answer

Gameobjects spawn differently on client and host. 0 Answers

Spawn gameobjects from list when grip button is clicked? 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