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

Naming a variable

Using the example of the mailbox, if I asked you to see what is in the mailbox, the first thing you'd ask is, "Which one?" If I say in the Smith mailbox, the brown mailbox, or the round mailbox, you'll know exactly which mailbox to open to retrieve what is inside it. Similarly, in scripts you have to give your variables a unique name. Then I can ask you what's in the variable named myNumber, or whatever cool name you might use.

Let's see how this is represented in our code. The first thing we need to do is create a new script in Unity, all the fun and magic starts here from these first steps:

  1. In the Unity project panel, under the Assets tab, we are going to rightclick the empty space:
  1. Then we go to the Create menu and select the C# Script option

  1. A new file was created and it is ready to be renamed; this is very important and we need to always give a name to this file. For now, we can call it variableScript (the name we gave to this file doesn't interfere with the content on it, so we can we choose any name we want):
  1. Then we double-click the script file that we have just created.
  2. The MonoDevelop program will open with the script ready to edit:
  1. Make sure that the name that appears after public class is exactly the same name that you assigned inside Unity (in this example, we gave the name variableScript). In case we don't rename the script file right away when it gets created, Unity will automatically assign the NewBehaviourScript name:
  1. Now we are ready to create our first variable, we are going to name it myNumber. Make sure that your script looks identical to the following screenshot (for now, don't be concerned about the details of how to write this):
  1. Save the file.

The golden rule: When you name variables, try to come up with a name that most accurately describes what value your variable contains. Avoid generic names such as name, speed, and score. Instead, name them playerName, carSpeed, and opponentScore, respectively.

主站蜘蛛池模板: 泸州市| 寿阳县| 金山区| 龙泉市| 固原市| 无棣县| 新巴尔虎右旗| 信阳市| 花莲市| 寿光市| 榆中县| 昌邑市| 新蔡县| 宁南县| 梁河县| 喀喇| 隆尧县| 东丰县| 临泉县| 仙桃市| 水城县| 宜都市| 靖边县| 肃南| 屏东市| 屏东市| 图木舒克市| 彝良县| 吉隆县| 寻乌县| 沙雅县| 长宁县| 方正县| 华亭县| 云安县| 阿尔山市| 安康市| 淮安市| 云浮市| 海门市| 安岳县|