- Home /
WaitForSeconds() Is not working.
In my code whenever I click a prefab is instantiated (shooting). But i want a cool down town timer so i added the WaitForSeconds method but its not working. What is the problem. Here is my code:
void Update () {
if(Input.GetMouseButton(0))
{
StartCoroutine( Shoot());
}
}
IEnumerator Shoot()
{
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
yield return new WaitForSeconds(5.0f);
}
Answer by DiegoSLTS · Jul 08, 2015 at 07:10 PM
Coroutines are not used for that, they don't prevent a piece of code from being called, you're starting a new Shoot coroutine on every Update where the condition is true (the mouse button being down).
A coroutine is just a method like every other, but it can stop at some point and continue from there later. In your case your coroutine is instantiating the laser, then stops for 5 seconds and then it finishes.
I'd do it without a coroutine, just store the time of the previous shoot and check if it passed enough time in the if condition. Something like this:
public float lastShootAt;
void Update () {
if(Input.GetMouseButton(0) && Time.time >= lastShootAt + 5f)
{
lastShootAt = Time.time;
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
}
}
Answer by sillyjake · Jul 08, 2015 at 09:56 PM
Here, Try this:
public bool canShoot = true;
void Update () {
if(Input.GetMouseButton(0) && canShoot) {
StartCoroutine( Shoot());
}
IEnumerator Shoot() {
canShoot = false;
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
yield return new WaitForSeconds(5.0f);
canShoot = true;
}
}
Answer by Daphoeno · Jul 08, 2015 at 07:00 PM
have you tried adding a boolean to activate and deactivate the cooldown either end of the IEnumeration to prevent it being called again? just adding a WaitForSeconds will not prevent the Coroutine from being called again....
Try this...
public bool _cooldown = false;
void Update () {
if(Input.GetMouseButton(0) && _cooldown == false)
{
StartCoroutine( Shoot());
}
}
IEnumerator Shoot()
{
_cooldown = true;
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
yield return new WaitForSeconds(5.0f);
_cooldown = false;
}
This works, but I think the Shoot coroutine does more than Shooting. Something like this might be clearer:
public bool _cooldown = false;
void Update () {
if(Input.Get$$anonymous$$ouseButton(0) && _cooldown == false)
{
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
StartCoroutine(Cooldown());
}
}
IEnumerator Cooldown()
{
_cooldown = true;
yield return new WaitForSeconds(5.0f);
_cooldown = false;
}
Answer by ei1 · Jul 08, 2015 at 09:57 PM
I don't think you need to say StartCoroutine. Try just saying
Shoot();
instead of
StartCoroutine(Shoot());
Also try saying just
yield WaitForSeconds(5.0f);
if that doesn't work, I would switch to javascript. I switched from c# to javascript and its much faster.
function Update () {
if(Input.GetMouseButton(0))
{
Shoot();
}
}
function Shoot()
{
Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
yield WaitForSeconds(5);
}
First, your suggestions and the code you posted have the same problems that the OP is trying to solve. Anyway, in C# you need the "StartCoroutine" bit, and you need the complete "yield return new Wait..." line. Those things you suggested only work with UnityScript.
Also, UnityScript might be faster to write (you can ignore types and some keywords) but as for the actual execution speed using one language or the other, it depends on the generated code. It's usually the same speed, but UnityScript can be slower. Look here: http://answers.unity3d.com/questions/7567/is-there-a-performance-difference-between-unitys-j.html
So, switching to UnityScript because some code doesn't work is a bad suggestion.