官术网_书友最值得收藏!

Using access modifiers

Now that the basic syntax is no longer a mystery, let's get into the finer details of variable statements. Since we read code from left to right, it makes sense to begin our variable deep-pe with the keyword that traditionally comes first – an access modifier.

Take a quick look back at the variables we used in the preceding chapter in LearningCurve and you'll see they had an extra keyword at the front of their statements: public. This is the variable's access modifier. Think of it as a security setting, determining who and what can access the variable's information.

Any variable that isn't marked public is defaulted to private and won't show up in the Unity Inspector panel.

If you include a modifier, the updated syntax recipe we put together at the beginning of this chapter will look like this:

accessModifier dataType uniqueName = value;

While explicit access modifiers aren't necessary when declaring a variable, it's a good habit to get into as a new programmer. That extra word goes a long way toward readability and professionalism in your code.

Choosing a security level

There are four main access modifiers available in C#, but the two you'll be working with most often as a beginner are the following:

  • Public: This is available to any script without restriction.
  • Private: This is only available in the class they're created in (which is called the containing class). Any variable without an access modifier defaults to private.

The two advanced modifiers have the following characteristics:

  • Protected: Accessible from their containing class or types derived from it
  • Internal: Only available in the current assembly

There are specific use cases for each of these modifiers, but until we get to the advanced chapters, don't worry about protected and internal.

Two combined modifiers also exist, but we won't be using them in this book. You can find more information about them at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers.

Let's try out some access modifiers of our own!

Time for action – making a variable private

Just like information in real life, some data needs to be protected or shared with specific people. If there's no need for a variable to be changed in the Inspector window or accessed from other scripts, it's a good candidate for a private access modifier.

Perform the following steps to update LearningCurve:

  1. Change the access modifier in front of currentAge from public to private and save the file.
  2. Go back into Unity, select the Main Camera, and take a look at what changed in the LearningCurve section.

Since currentAge is now private, it's no longer visible in the Inspector window and can only be accessed within the LearningCurve script. If we click Play, the script will still work exactly as it did before:

This is a good start on our journey into variables, but we still need to know more about what kinds of data they can store. This is where data types come in, which we'll look at in the next section.

主站蜘蛛池模板: 新源县| 嘉鱼县| 墨江| 临江市| 赣州市| 大埔县| 会泽县| 达孜县| 沁阳市| 安福县| 秦安县| 六安市| 松原市| 保靖县| 佳木斯市| 纳雍县| 瑞金市| 云南省| 南平市| 霍城县| 安丘市| 神池县| 琼海市| 三河市| 霍山县| 眉山市| 东乡| 抚顺县| 武汉市| 贵定县| 枞阳县| 康定县| 三原县| 沧州市| 白城市| 法库县| 天镇县| 寻甸| 大关县| 孟州市| 洞头县|