Why Coroutine local variable doesn't work?

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: