Chapter 6.  Using Dot Syntax for Object Communication

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:

  • Dot Syntax being just an address
  • Working with objects
  • Using Dot Syntax in a script
  • Accessing GameObjects using drag-and-drop versus writing code

So let's get on with it...

Using Dot Syntax is like addressing a letter

Ever seen something like this?

www.unity3d.com

That's right, a web address. Gee, I wonder why it's called a web address?

Simplifying the dots in Dot Syntax

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:

Simplifying the dots in Dot Syntax

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?

Using access modifiers for variables and methods

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:

  • A variable will not show in the Inspector as a Component property
  • The variable or method will not be accessible from other scripts

We can specify a variable or method to be public.

Here, public means:

  • A variable will show in the Inspector as a Component property
  • The variable or method will be accessible from other scripts

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.

Note

A script always has access to its own member variable and methods whether they're private or public.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181