-
Notifications
You must be signed in to change notification settings - Fork 0
03 Variables
All values are stored as groups of 0-1 bit values
Variable names can consist of: letters (any combination of uppercase/lowercase), numbers, underscore.
Variable names are case sensitive. MyVariable is different from myVariable
The following are NOT allowed:
Variable names starting with a numberSpaces, punctuation, math symbols
- Every variable needs a home. There are no global variables in C#.
- Variables are only valid within the code block where they are declared and any of it's contained code blocks.
- Variables are not valid above where they are declared within a code block.
Document comparing scratch variables with C# variables
| Category | Common | Type | Bits | Range | Description |
|---|---|---|---|---|---|
| True/False | * | bool | 8 | true or false | Only one bit is used. |
| Whole Numbers | byte | 8 | 0 - 255 | Unsigned 8 bit number | |
| sbyte | 8 | -128 - 127 | Signed 8 bit number | ||
| short | 16 | -32,768 - 32,767 | Signed 16 bit number | ||
| ushort | 16 | 0 - 65,535 | Unsigned 16 bit number | ||
| * | int | 32 | ±2,147,483,648 | Signed Whole numbers up to 2 billion. | |
| uint | 32 | 0 - 4 billion | Unsigned whole numbers up to 4 billion. | ||
| long | 64 | ±9,223,372,036,854,775,807 | Crazy big whole numbers. | ||
| ulong | 64 | ±18,446,744,073,709,551,615 | Crazy big positive only whole numbers. | ||
| Fractional Numbers | decimal | 128 | ±1x10-28 - ±8x1028 | Used for money or where there are a fixed number of decimal places. Very precise, up to 29 significant digits. | |
| * | float | 32 | ±3 x 1028 | Not as accurate as double, but faster and half the memory. Often used in games. | |
| double | 64 | ±2 x 10308 | Much more accurate than float, but slower and twice as much memory. Rarely used in games. | ||
| Characters | char | 8 | Any one Ascii character | A single letter or symbol | |
| Time | DateTime | Any time | Represents a date and time | ||
| Unity Specific | * | Vector2 | 2 * 32 | Same as float | Stores a two dimensional x,y position or offset |
| * | Vector3 | 3 * 32 | Same as float | Stores a three dimensional x,y,z position or offset as 3 floats | |
| * | Quaternion | 3 * 32 | Same as float | Stores a three dimensional rotation |
| Category | Common | Type | Bits | Range | Description |
|---|---|---|---|---|---|
| Characters | * | string | (varies) | Ascii or Unicode characters | One or more symbols or words |
Any type can be made nullable. The nullable variable itself will be a value type (since it is a struct) even if the value it contains may be a reference type. In the variable declaration, appending a Question Mark ? character after the type converts the type to a nullable type.
int? myFirstInt = 1; // Value is 1
int? myOtherInt = null; // Value is null (not 0)
int myBadInt = null; // Error - not nullableThis is a custom type you can create to store a list of values. As far as the computer is concerned, these are just int numbers. The named values are to make the code easy to read and understand.