• 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 /
This question was closed May 15 at 09:04 PM by Nakoru.
avatar image
0
Question by Nakoru · Sep 16, 2021 at 06:30 PM · instantiateinstantiate prefabcountdownautomaticrate

Countdown not working (instantiating on count<=0)

Hey. I made tower-defense automatic shooting system, the problem is that my bullets are non-stop created, and I saw that my fireCountdown float doesn't set back to the fireRate, and stays at 0 making my attack script non-stop-spawning bullets.

Do you have an idea of how to fix that ? The Update part code ;

 void Update () {
         if (target == null)
         {
             if (useLaser)
             {
                 if (lineRenderer.enabled)
                 {
                     lineRenderer.enabled = false;
                     Destroy(impactEffect);
                     impactLight.enabled = false;
                 }
             }
 
             return;
         }
 
         if (useLaser)
         {
             Laser();
         } 
         else // if using bullets
         {
             if (fireCountdown <= 0f)
             {
                 Shoot();
                 fireCountdown = fireRate;
             }
 
             fireCountdown -= Time.deltaTime;
         }
     }



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

  • Sort: 
avatar image
0
Best Answer

Answer by Nakoru · Sep 16, 2021 at 07:43 PM

Fixed by moving the countdown line over the Shoot() call 0-0.

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 Kona · Sep 16, 2021 at 07:21 PM

Are you sure that your "fireRate" property is not set to 0? As far as I can see that code should work. Another way to do it is to cache the last time a bullet was fired, something like this:

 public float attackRate = 1f;  // Fire one projectile/sec
 private float _lastAttack;
 
 public void Awake() {
   _lastAttack = Time.time;
 }
 
 public void Update() {
   if( Time.time - _lastAttack >= attackRate ) {
     Shoot();   //Shoot a bullet when the target time has passed
     _lastAttack = Time.time;  //Cache current time again
   }
 }
Comment
Add comment · Show 1 · 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 Nakoru · Sep 16, 2021 at 07:42 PM 1
Share

Okay, I don't figure out why, but moving the line of the countdown = fireRate over the Shoot() call made it work. Thanks for your time tough, it's really nice of yours !

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

150 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

Related Questions

Projectiles trigger affecting other projectiles of same prefab - C# 0 Answers

JS: How to instantiate a prefab from a public class? 1 Answer

Spawning prefabs in front of the player 1 Answer

Trying to instantiate a ball at the cursor position 1 Answer

Show GUI Texture on Trigger and Instantiate Object if Button pressed while In Trigger? 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