- Learning C# by Developing Games with Unity 3D Beginner's Guide
- Terry Norton
- 263字
- 2025-04-04 22:23:20
Components communicating using the Dot Syntax
Our script has variables to hold data, and our script has methods to allow tasks to be performed. I now want to introduce the concept of communicating with other GameObjects and the Components they contain. Communication between one GameObject's Components and another GameObject's Components using Dot Syntax is a vital part of scripting. It's what makes interaction possible. We need to communicate with other Components or GameObjects to be able to use the variables and methods in other Components.
What's with the dots?
When you look at the code written by others, you'll see words with periods separating them. What the heck is that? It looks complicated, doesn't it. The following is an example from the Unity documentation:
transform.position.x
Note
Don't concern yourself with what the preceding code means as that comes later, I just want you to see the dots.
That's called the Dot Syntax. The following is another example. It's the fictitious address of my house: USA.Vermont.Essex.22MyStreet
Looks funny, doesn't it? That's because I used the syntax (grammar) of C# instead of the post office. However, I'll bet if you look closely, you can easily figure out how to find my house. We'll get into much more Dot Syntax detail in Chapter 6, Using Dot Syntax for Object Communication.
Pop quiz – knowing the C# building blocks
Q1. What is the purpose of a variable in a script?
Q2. What is the purpose of a method in a script?
Q3. How does a script become a Component?
Q4. What is the purpose of Dot Syntax?