Data Types

Today in Ant’s lesson we learned the different C# data types in Unity.

There are 4 main data types we learned about; Integers, Floating points, Booleans, and Strings.

Integers are whole numbers in code, which can be used to do things such as move an asset by a certain amount of pixels/second. In code these variables are written as ‘int’.

Floating points are decimals, which tend to be used in 3D games for things such as rotations. In code these variables are written as ‘float’.

Booleans are usually 2 possible values, where you have one or the other; for example, if you have 1 your character could be classed as alive, whereas 0 is dead (and the game would be over). Booleans are written as ‘bool’ in Unity and as ‘Number’ in Flash.

Strings are chains of characters used to reference something, such as ‘(“Vertical”) in a transform script. Strings are declared in Unity by ‘string’, and by ‘String’ in Flash.

All of these variables can be made available to edit in Unity editor (outside of the script alone) by making them ‘public’ which just comes before the data type. They will then be viewable/editable in the Inspector.

Here are example lines of c# script I wrote to practice writing the different data types.

datatypesWe then implemented some of this into a space game we have started to make to control the rotation, engine power and thrust of the ship character in the game.

shipscript.pngThis made the ship accelerate forward when hitting the up arrow key, and rotate clockwise and anticlockwise with the right and left arrow keys.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s