Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kevinseligmann · Aug 23, 2012 at 08:12 PM · c#bulletienumeratorcooldown

C# Cooldown not working

Hello everyone.

After some months of working with Unityscript, I decided to swith to C# and I'm finding it somehow more difficult. Currently I'm experiencing a problem trying to make a cooldown for one of my games.

Code: using UnityEngine; using System.Collections;

 public class BulletConstructor{
 
     public float BulletDamage;
     public float BulletCooldown;
     
     private bool canFire = true;
     private float CooldownProgress = 0.0f;
 
        // constructor declaration
     public BulletConstructor(float rBulletDamage, float rBulletCooldown){
         this.BulletDamage = rBulletDamage;
         this.BulletCooldown = rBulletCooldown;
     }
     
     public bool Fire(){
         
         if(canFire){
             canFire = false;
             SetCooldown();
             return true;
         }else{
             return false;    
         }
         
     }
     
     private IEnumerator SetCooldown(){
         
         Debug.Log ("We enter the cooldown");
         
         float counter = 0.0f;
         while( counter < BulletCooldown ){
                 
             CooldownProgress = counter / BulletCooldown;
             yield return null;
             counter += Time.deltaTime;
         }
     }
 }

I don't get any errors, but for some reason the function SetCooldown is never accesed. I don't see the Debug.Log on the console when the function starts.

Just to clarify, I'm calling this class from other script using the following:

 private BulletConstructor Bullet1;
 Bullet1 = new BulletConstructor(100f, 25f);
 
 if(Bullet1.Fire()){
     Debug.Log("We just shot");
 }else{
     Debug.Log("We're still on cooldown");
 }


So, what am I missing here?

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

3 Replies

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

Answer by Sundar · Aug 23, 2012 at 11:30 PM

Try this

 public class BulletConstructor : MonoBehaviour{
 
     public float BulletDamage;
     public float BulletCooldown;
 
     private bool canFire = true;
     private float CooldownProgress = 0.0f;
     
 
     public bool Fire(float rBulletDamage, float rBulletCooldown){
        this.BulletDamage = rBulletDamage;
        this.BulletCooldown = rBulletCooldown;
        if(canFire){
          canFire = false;
          StartCoroutine( SetCooldown() );
          return true;
        }else{
          return false;    
        }
 
     }
 
     private IEnumerator SetCooldown(){
 
        Debug.Log ("We enter the cooldown");
 
         float counter = 0.0f;
         while( counter < BulletCooldown ){
 
             CooldownProgress = counter / BulletCooldown;
             yield return null;
             counter += Time.deltaTime;
         }
     }
 }
 
 
 ............
 // call 
 private BulletConstructor Bullet1;
 Bullet1 = new BulletConstructor();
 
 if(Bullet1.Fire(100f, 25f)){
     Debug.Log("We just shot");
 }else{
     Debug.Log("We're still on cooldown");
 }
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 kevinseligmann · Aug 24, 2012 at 12:03 AM 0
Share

Thanks very much @Sundar , now I understand how the classes work and what to do with a IEnumerator on C#. The code worked flawlessly, and I modified it to suit my needs.

Thanks again!

avatar image Sundar · Aug 24, 2012 at 03:08 PM 0
Share

You are welcome.

avatar image
1

Answer by WilliamLeu · Aug 23, 2012 at 10:00 PM

The IEnumerator returned from SetCooldown() needs to be passed to a MonoBehavior, via MonoBehavior's StartCoroutine() function . Without that, the function is just going to stop and reset after the first yield return instead of pausing and continuing between frames.

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 kevinseligmann · Aug 23, 2012 at 10:18 PM 0
Share

I'm sorry, but I'm a little confused because I'm new to classes and C#. $$anonymous$$i class does not extends $$anonymous$$onobehaviour, because I read that it doesn't work well with calling the constructor like I did. Nevertheless, how am I suposed to to pass the return value from the function into a Coroutine? I really don't get that part.

Thanks for taking the time

avatar image
0

Answer by Sundar · Aug 23, 2012 at 09:27 PM

Your class name and constructor name should be same. Change your class name "SpellConstructor" to "BulletConstructor"

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 kevinseligmann · Aug 23, 2012 at 09:42 PM 0
Share

I'm sorry, I've modified the script earlier and the class name is the same as the constructor. Anyway, it's still happening. It never enters the SetCooldown function.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

collider doesnt react to script 0 Answers

No Glitch Please ... 1 Answer

Rotate a camera when my flashlight is on the edge of the screen... Video included 2 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