- Learning C# by Developing Games with Unity 5.x(Second Edition)
- Greg Lukosek
- 469字
- 2021-07-09 20:03:35
Declaring a variable and its type
Every variable that we want to use in a script must be declared in a statement. What does that mean? Well, before Unity can use a variable, we have to tell Unity about it first. Okay then, what are we supposed to tell Unity about the variable?
There are only three absolute requirements to declare a variable and they are as follows:
- We have to specify the type of data that a variable can store
- We have to provide a name for the variable
- We have to end the declaration statement with a semicolon
The following is the syntax we use to declare a variable:
typeOfData nameOfTheVariable;
Let's use one of the LearningScript
variables as an example; the following is how we declare a variable with the bare minimum requirements:
int number1;
This is what we have:
number1
can store, which in this case is anint
, meaning an integer
is the type of data that - Requirement #2 is a name, which is
number1
- Requirement #3 is the semicolon at the end
The second requirement of naming a variable has already been discussed. The third requirement of ending a statement with a semicolon has also been discussed. The first requirement of specifying the type of data will be covered next.
The following is what we know about this bare minimum declaration as far as Unity is concerned:
- There's no public modifier, which means it's private by default
- It won't appear in the Inspector panel or be accessible from other scripts
- The value stored in
number1
defaults to zero
The most common built-in variable types
This section shows only the most common built-in types of data that C# provides for us and variables can store.
Only these basic types are presented here so that you understand the concept of a variable being able to store only the type of the data that you specify. Custom types of data, which you will create later, will be discussed in Chapter 7, Creating the Gameplay is Just a Part of the Game in the Discussion on Dot Syntax.
The following chart shows the most common built-in types of data you will use in Unity:

Note
There are a few more built-in types of data that aren't shown in the preceding chart. However, once you understand the most common types, you'll have no problem looking up the other built-in types if you ever need to use them. You can also create your own classes and store their instances in variables.
We know the minimum requirements to declare a variable. However, we can add more information to a declaration to save time and coding. In LearningScript
, we've already seen some examples of assigning values when the variable is being declared, and now we'll see some more examples.
- DevOps:軟件架構師行動指南
- Learning Python Web Penetration Testing
- C++面向對象程序設計(微課版)
- Python金融數據分析
- UML+OOPC嵌入式C語言開發精講
- 21天學通C++(第6版)
- Hands-On Swift 5 Microservices Development
- Learning Salesforce Einstein
- UML 基礎與 Rose 建模案例(第3版)
- iOS自動化測試實戰:基于Appium、Python與Pytest
- Python機器學習算法與應用
- Cocos2d-x Game Development Blueprints
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Ionic3與CodePush初探:支持跨平臺與熱更新的App開發技術