- Learning C# 7 By Developing Games with Unity 2017(Third Edition)
- Micael DaGra?a Greg Lukosek
- 339字
- 2021-07-02 19:59:36
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:
- We start by selecting the script that we have created and then we double–click it to open inside MonoDevelop:

- 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:

- After the void Start () function, we are going to write the math equation total = 2 + myNumber:

- Save the file, go back to the Unity program, and drag and drop the script file on top of the Main Camera object:

- 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!
- Java程序設計(慕課版)
- Android Jetpack開發:原理解析與應用實戰
- ASP.NET Core 2 and Vue.js
- HTML5 Mobile Development Cookbook
- Programming ArcGIS 10.1 with Python Cookbook
- MySQL 8 DBA基礎教程
- 云計算通俗講義(第3版)
- Monitoring Elasticsearch
- C語言程序設計
- Learning Hunk
- Learning Three.js:The JavaScript 3D Library for WebGL
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Beginning C++ Game Programming
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- HikariCP數據庫連接池實戰