- [Instructor] The scope of a variable is the area
in code which the variable can be used in.
A variable is said to be local to the place
in code that it can be used.
Code blocks are generally what defines a variable's scope,
and these are denoted by braces.
For example, everything within the class here
can be said to be local to that class.
You would say that the variables alpha,
beta and gamma are in scope
within the scope and access modifiers class.
And you would say that the pens, crayons
and answer variables are in scope
within the example function.
Let's talk about Public and Private Access Modifiers.
Variables defined in the class as opposed
to those declared within a function have an access
modifier attributed to them.
An access modifier is a keyword placed before a data type
when declaring the variable and it's purpose is
to define where the variable or function can be seen from.
As a general rule if other scripts need access
to a variable or function then it should be public,
otherwise is should be private.
Declaring a variable as public means that it can be accessed
from outside the class.
It also means that the variable is shown
and editable on the component in the Inspector.
In this example my scope and access modifiers script
is applied to our prop skate bot object
and you can see that the public variable
alpha is included as a property that I can edit.
This allows the user to edit the variable
while they test the game.
Imagine, for example, the value controls the speed of a car.
It would be nice to be able to tweak that variable
while testing it without having to stop,
edit the script and play again.
As such, it makes sense to have this be a public variable.
Note that if a variable is initialized in the class
to a a default value, for example, if we had put alpha
equals five then it will still be overridden by the value
that's written in the Inspector.
However if these values are set in functions,
such as Start and Awake, these occur
after the variable has been set in the Inspector
and will not be overridden by the Inspector.
So for example, if my script had a default value of five
but was being overridden by the Inspector,
and then setting it manually in the Start function,
that will override the value
that was input in the Inspector.
Now, given that the game is running,
if I tweak the value again,
that will override the value in the script again.
Importantly, the values modified via the Inspector
while the game is in Play mode will be reset and not saved
in the Inspector when we exit Play mode.
Even though the public variable is tweakable
on the component it doesn't mean that you can't go back
and change it via a script at runtime.
Private variables can only be edited from within the class.
In C#, private is the default access modifier
for any variable that doesn't have it specified.
So, although I've written private on beta and gamma,
if I hadn't they would be private regardless.
It's good practice to make all member variables,
those that belong to a class rather than a function,
private, unless they need to be public
for a specific reason.
Setting variables and functions to public can
also mean that you can access them from other scripts.
So for example, I have another class here
which has two functions in a public function called
"FruitMachine' and a private function called "OfficeSort."
I also have some public variables
and some private variables.
Back in my original script,
I can only access the public members of that script.
For example, if I were to create an instance
of the class "AnotherClass," you can see that when I try
and access this the "apple" and "banana" variables
as well as "FruitMachine" are available,
and you can see that in telesense in Visual Studio
is telling me that I can use these.
When I choose FruitMachine, it's then showing me
what arguments can be fed in to that function.
Now, I can feed in any of the variables
that are in scope of this script,
or that are in scope of "AnotherClass" because they're public.
So I could use any of the variables
from my scope and access modifiers class,
and I can access the instance of another
class and make use of a public variable
from there, such as "apples," for example.
Note that because "OfficeSort" and "stapler"
and "sellotape" are all private,
I can't access them by creating
an instance of another class.
They are for use only within that class itself.