Question – Variables in functions & private and public functions
You could put all of your variables at the top of a script. But that can turn into a jumble. To make programs easier to write, we invented a rule called _local_ variables. Those only exist in their function, which means the name isn’t used up and can’t hit another name. It’s a common language thing and you can look up non-Unity examples
The easiest way to see how nice this is, imagine you borrow a big function a friend wrote. It uses variables n, x, and more. But you’ve also used n and x in some of your functions. Hmmm… . But it’s not a problem since everything is local (as in
void myFunc() { int n, float x; ... }
. In the old days, with everything declared outside of functions, you’d have to change the names.
Read more here: Source link
