Why Coroutine local variable doesn't work?

In my code, I made a local variable of type coroutine as below :-

    void ShootLaser()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Coroutine laserCoroutine = StartCoroutine(FireContinuosly());
        }
        else if(Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(laserCoroutine);
        }
    }

But this local laserCoroutine isn’t working…WHY?
And why do even i need to store it in a variable? just typing StopCoroutine(FireContinously()); won’t work??

Also I’m not getting what is the use of yield keyword before the return keyword and y do we use new keyword in various places(like WaitForSeconds and Vector)

One more thing…why Coroutines use a special way to start( StartCoroutine() ) and same for stopping it? Why can’t simply calling them completes our work?? And why is the type of Coroutines functions is IEnumerator and not Coroutine?

Bunch of questions :sweat_smile: plz help

Hi Abhinav,

Check the first if-block your ShootLaser method. What exactly is happening there?

Regarding your screenshot, if the FireContinously method spelt correctly? Or doesn’t the red line indicate a problem?

And why is the type of Coroutines functions is IEnumerator and not Coroutine ?

Because the Unity programmers made it this way. The StartCoroutine method takes in an object of type IEnumerator.

Aah!..why unity programmers are like that.
Got this much, but I still want to know the use of new and yield keywords ?_?
I read the documents but didn’t understood the use of both of them.

Also the red line says - “Expensive method invocation”, my spellings are not that bad :slightly_smiling_face:

In C#, new is used to create a new object. yield is used in special C# methods that are able to “pause”. These are not Unity’s concepts but belong to the C# language.

Also the red line says - “Expensive method invocation”, my spellings are not that bad

Which line says that? If it just refers to FireContinuosly, you can probably ignore the warning.

Regarding your problem, did you manage to solve it? If not, here is a hint: What is “scope”?

Yup, I got the problem with that variable and understood yield. However new is still new for me, but maybe I will understand it later. Also I can ignore that red line.

One more thing - If we want to access a variable, say spawnTime in a scriptable object, then why we always make a method with a return type of that variable, why can’t we directly access the variable from that script. What’s the advantage of accessing a method rather than variable??

In this course, we aren’t using it that often in Unity because the Unity frameworks usually creates new objects for us. An example where we use it is new Vector2(1f, 1f, 1f);. Maybe you’ve already seen that somewhere.

We can do that but it is regarded as bad practice. The advantage of the method is that nothing but the instance itself can access the variable during runtime. In a little project like ours, the risks of exposed variables is not that obvious but imagine you would be working in a team. You don’t always check your colleagues code. It might be that they change the value of your public variable during runtime, by accident or on purpose. Assuming a problem occurs due to that and assuming this was a a large project with thousands of lines of code and dozens of different classes, you’ll have a hard time to find the bug.

For this reason, we only expose variable if absolutely necessary. That’ll make debugging easier because less potential sources of errors means less things to check.


See also:

Got It!
Thnx :))

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

Privacy & Terms