- Home /
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?
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");
}
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!
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.
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
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"
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
Follow this Question
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