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

A variable name is just a substitute for a value

As you write a script and create a variable, you are simply creating a placeholder or a substitute for the actual information that you want to use. Look at the following simple math equation: 2 + 9 = 11.

Simple enough! Now try the following equation: 11 + myNumber = ???. There is no answer to this. You can't add a number and a word. Going back to the mailbox analogy, write the number 9 on a piece of paper. Put it in the mailbox named myNumber. Now you can solve the equation. What's the value in myNumber? The value is 9. So now the equation looks normal: 11 + 9 = 20.

The myNumber variable is nothing more than a named placeholder that can store some data (information). So, wherever you would like the number 9 to appear in your script, just write myNumber and the number 9 will be substituted.

We can test this on the script that we had previously created, so let's do it:

  1. We start by selecting the script that we have created and then we doubleclick it to open inside MonoDevelop:
  1. Now we create a new variable called total and we don't need to assign any number to it because we want this variable to show us the result of our:
  1. After the void Start () function, we are going to write the math equation total = 2 + myNumber:
  1. Save the file, go back to the Unity program, and drag and drop the script file on top of the Main Camera object:
  1. Click Play and take a look at the Total variable:

Although this example might seem silly at first, variables can store all kinds of data that is much more complex than a simple number. This is just a simple example that shows you how a variable works. We will definitely look at more complex variable types at later stages. Remember, slow, steady progress, baby steps!

主站蜘蛛池模板: 锡林浩特市| 凯里市| 巫山县| 黑山县| 旌德县| 资溪县| 东宁县| 张北县| 日喀则市| 格尔木市| 宜城市| 宜丰县| 西和县| 吴忠市| 青冈县| 云阳县| 莎车县| 新巴尔虎右旗| 娱乐| 隆昌县| 苏尼特右旗| 芒康县| 肃北| 潜山县| 龙川县| 拜城县| 迭部县| 祁东县| 苍梧县| 新安县| 启东市| 垦利县| 龙泉市| 景德镇市| 陆川县| 磐安县| 盐山县| 肃南| 镇康县| 永城市| 兴和县|