Alternative approach to stop firing without StopCoroutine

I found the approach of using StopCoroutine from the update() method to stop the firing a little weird. Generally I would hope that the Coroutine could be as self-sufficient as possible. So given we already have a while loop around the whole firing block, I did the following, and it seems to work:

private IEnumerator FireContinuously()
{
    while (Input.GetButton("Fire1"))
    {
        [ ... standard shooting code ...]

        yield return new WaitForSeconds(shotFiringPeriod);
    }
}

Is there a reason why I wouldn’t want to use this self-contained approach instead? Or is it just a matter of Rick looking for a good opportunity to teach “StopCoroutine”?

If it’s the later, I understand. Just want to make sure I’m not missing something.

Hi,

I don’t see any problem with your solution. If it works, keep it. :slight_smile:

It is very likely that Rick did what he did to be able to show the StopCoroutine method. Or he figured that his approach was a nice solution. In many cases, there are multiple ways to make something work in Unity. He cannot show all of them.

So if you develop a solution and it works, keep it. Since nobody is a clairvoyant, do it like a real game developer: Don’t worry about problems you do not have. If your solution causes a problem at a later juncture, try to solve the issue. However, if your solution works, it works.

Did this help?

2 Likes

Thank you. Just wanted to be sure there wasn’t some hidden performance reason.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms