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 /
avatar image
0
Question by dumbkingdoughnut7 · Apr 10 at 07:12 AM · unity 2dtimerspawningspawning problemstimer countdown

How to stop spawning enemies after countdown timer reaches zero?

I'm trying to stop the enemy spawn when my countdown timer reaches zero so that the boss shows up but I don't know how to do it.

Here's my countdown timer code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Countdown_Timer : MonoBehaviour
 {
     float currentTime = 0f;
     float startingTime = 10f;
     [SerializeField] Text countdownText;
     
 
 
     // Start is called before the first frame update
     void Start()
     {
         currentTime = startingTime;
     }
 
     // Update is called once per frame
     void Update()
     {
         currentTime -= 1 * Time.deltaTime; //timer decreases by one each second
         countdownText.text = currentTime.ToString("0"); //conver float to string
 
         //stop the timer to zero
         
             
             if (currentTime <= 0) 
             {
                 currentTime = 0;
                 
             }
             
         
     }
 }


And here's my enemy spawner code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemySpawner : MonoBehaviour
 {
     public GameObject enemy;
     float randX;
     Vector2 whereToSpawn;
     public float spawnRate = 5f;
     float nextSpawn = 0.0f;
     bool spawn = true;
     public float timer;
 
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Time.time > nextSpawn)
         {
             nextSpawn = Time.time + spawnRate;
             randX = Random.Range(-18f, 18f);
             whereToSpawn = new Vector2(randX, transform.position.y);
             Instantiate(enemy, whereToSpawn, Quaternion.identity);
         }
 
         if (Time.time > timer)
         {
             StopSpawning();
         }
     }
     public void StopSpawning()
     {
         spawn = false;
     }
 
 }



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

1 Reply

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

Answer by Caeser_21 · Apr 10 at 07:50 AM

I think you just have to call public void StopSpawning() then stop the timer... Try this :

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class Countdown_Timer : MonoBehaviour
  {
      float currentTime = 0f;
      float startingTime = 10f;
      [SerializeField] Text countdownText;
      private bool HasReached;
      public EnemySpawner Spawner; //Assign this in the inspector
      
      void Start()
      {
          HasReached = false;
          currentTime = startingTime;
      }
  
      void Update()
      {
          if (HasReached == false)
          {
              currentTime -= 1 * Time.deltaTime; //timer decreases by one each second
              countdownText.text = currentTime.ToString("0"); //conver float to string
  
              //stop the timer to zero                     
              if (currentTime <= 0) 
              {
                  HasReached = true;
                  Spawner.StopSpawning();                 
              }
          }         
      }
  }

Then for the EnemySpawer script :

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EnemySpawner : MonoBehaviour
  {
      public GameObject enemy;
      float randX;
      Vector2 whereToSpawn;
      public float spawnRate = 5f;
      float nextSpawn = 0.0f;
      bool spawn = true;
      bool HasSpawnedBoss;
  
      void Start()
      {
          HasSpawnedBoss = false;
      }
  
      void Update()
      {
          if (Time.time > nextSpawn && spawn == true)
          {
              nextSpawn = Time.time + spawnRate;
              randX = Random.Range(-18f, 18f);
              whereToSpawn = new Vector2(randX, transform.position.y);
              Instantiate(enemy, whereToSpawn, Quaternion.identity);
          }
          else if (spawn == false && HasSpawnedBoss == false)
          {
              HasSpawnedBoss = true;
              //Add your boss spawning script here
          }
      }
 
      public void StopSpawning()
      {
          spawn = false;
      }
  }

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 dumbkingdoughnut7 · Apr 10 at 08:04 AM 0
Share

It works! Thank you so much again!

avatar image Caeser_21 dumbkingdoughnut7 · Apr 10 at 08:16 AM 0
Share

Glad I was able to help :)

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

149 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

Related Questions

Side scroller spawns at different Y 1 Answer

countdown timer starts from the begining when i reLaunch the app 1 Answer

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

Timer Progress Bar 2 Answers

how to Subtract string from TimeSpan 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