Lesson 7: Scope and Access Modifiers

From this lesson moving forward, we’re going to be speaking of C# scripting in a way that more relates to its functions within Unity. Scope and Access Modifiers is a fantastic place to begin.

Before we begin, let’s set our state of mind to the following:
When you are writing scripts for your game, whether it be behavior scripts for character movement, physics, or shooting a gun, we can safely say that it will be necessary for these systems to interact.

What is a Scope?

The scope of a variable is the area in the code in which the variable can be used in. A variable is local to the place in code that it can be used. Code blocks are generally the area that defines where the code can be used, and they are denoted by braces/brackets {}.

Examples of Scope:

Screen Shot 2017-07-06 at 3.40.41 PM
Notice the ending and beginning brackets/braces {} for the class we have highlighted. This is the scope for this class. This means the variables we created on top coins, powerups, extralife ) can be used by any other Method/Variable within this block of code Start(), Update(), and the if() statement within Update() ). The scopes of Update() and if() statement, however, are out of our scope. 
3

The scope of Update(), like any other scope, is also defined by its brackets {}. However, the key difference is that this scope is also within the scope of the class. This means we inherit and have access to Methods/Variables within this class. What we do not have access to is the scope of the if() statement, as it is out of our scope.
2

 

Lastly, we have the scope of the if() statement. I will repeat myself to pummel it into the mind: the scope is defined by the brackets/braces {}. In this case, the scope of the if() statement inherits and has access to the scope of Update() and the class. The class and Update() scopes, however, do not have access to any code created within the if() statement. 

 


What is an Access Modifier?

An Access Modifier is a keyword placed before a data type when declaring the variable. You may have noticed many examples of this:

public int coins = 1; // the access modifier here is "public"
private int powerups = 2; // the access modifier here is "private"

Here we see two examples of Access Modifiers:
public: Other scripts have access to this object/function/variable.
private: No scripts outside of its scope has access to its object/function/variable.

As a rule of thumb, you should make something public if other scripts need access to it, and private if no scripts outside of the scope need access to it. 

As an example, imagine you had a variable for health with a value of int 100 assigned to it. In this game, if an enemy hits you, that enemies script needs access to your health variable to then subtract the amount of damage it dealt to the health. This needs to be set to public in order for it to operate smoothly. Otherwise, if the health variable were set to private, no script would have access to it and the health would never decrease.

One of the most useful things about making a variable public is that Unity picks this up in the Editor and let’s you make on the fly alterations to the variable. For example, you can set your game’s gravity variable to public, thus allowing you to alter it while testing without having to dip your hands back into the code.

Example of the on-the-fly edits you can make to a variable in Unity’s Editor when you make it’s Access Modifier public.

Conclusion:

Scopes and Access Modifiers may sound abstract to the beginner, and it did for me as well! I chose to compare it to the levels of clearance in the military. Some intel/files are only accessible to certain ranks – for obvious reasons. The wrong info in the wrong hands can be devastating! And concurrently, some intel is so crucial to everyone in the military that making it public is the most logical approach! Imagine if a Captain sent a squad of soldiers to scout a specific location while concurrently knowing it was filled to the brim with land mines — that would be just as devastating! Just as the military picks and chooses the most logical information to make public or private — we must analyze our code to decide what variable needs to be public or private.

Study Resources:
https://unity3d.com/learn/tutorials/topics/scripting/scope-and-access-modifiers?playlist=17117

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s