Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by ShinJohnSnyder · Mar 22 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 this 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. This 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 thisSpawn, float spawnRate)
     {
         if (KaijuLevelManager.instance.enemiesActive && currentWaveNumber >= waveStart)
         {
             if (waitToSpawn <= 0)
             {
                 GameObject enemyClone = Instantiate(enemiesToSpawn[thisSpawn], transform.position, transform.rotation);
                 waitToSpawn = spawnRate;
             }
         }
     }
 }

This 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 this 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 this.

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

The problem I run into when trying to do this is that "thisWaitToSpawn" will not reset itself to equal "spawnRate". If I try to put "thisWaitToSpawn" 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 this point I'm pretty much banging my head on the wall trying to figure this 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.

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

How do I make the enemies continue spawning? 1 Answer

Putting a cap on enemies spawned 2 Answers

Gameobjects spawn differently on client and host. 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