Scripts do many things by accessing the features built into Unity and third-party plugins. The Unity Scripting Reference is our link to the built-in Unity features. The thing is, exactly how do we invoke all of those Unity features?
So far all we've covered is basic C# programming. Granted, the example code we've seen has included some Dot Syntax, such as Debug.Log() to send output to Unity's Console. This is one of those Unity features. In the last chapter, we even saw some more Dot Syntax, pony.Key and pony.Value, which has nothing to do with Unity. These are just C# OOP (Object Oriented Programming) related features.
In both cases, there's some type of communication taking place to access methods and data to make things happen. To the beginner, those dots maybe odd looking and confusing, and they may ask, "What's the deal with all those darn dots between words?" Well, if you've been using the Internet and paid any attention at all, you've been using those dots, probably for years, and didn't pay much attention to them.
We see how to access the power of Dot Syntax as we cover the following sections:
So let's get on with it...
Ever seen something like this?
That's right, a web address. Gee, I wonder why it's called a web address?
Here is a fictitious mailing address:
Terry Norton
22 myStreet
Essex, VT
You've understood how to read an address like this since you were a kid. Let's take a look at it again using a different format:
Looking at that, Dot Syntax isn't so confusing. It's just an address in a different format, in a way to locate things. Here's an example: imagine we met in Europe somewhere, and I ask you to get my sunglasses using only this information:
USA.Vermont.Essex.22 myStreet.2ndFloor.office.desk.center drawer.sunglasses
Would you have any problem locating them?
I could foresee one big issue trying to retrieve my sunglasses. My house isn't open to the public; it's a private residence, so the door is locked. This means you don't have access to the sunglasses.
The same rules of access apply to member variables and methods of a class or script.
In C#, when we create a member variable or method in a script, it is private by default. We can also explicitly specify that it's private
.
Here, private
means:
We can specify a variable or method to be public
.
Here, public
means:
Dot Syntax is the system used to locate and communicate with a variable or method in an object. To understand how to use Dot Syntax, we have to know the relationship between a class and its objects.
18.119.28.108