LinkedInTwitterFacebook

Programming with C# and Unity 3D game engine – part 001 .

This tutorial will cover the first steps programming with C#.
The C# is an more complicated language like another programming language.
Now, C# it is an object-oriented programming language like C++ or Java.
If you know this programming language then will be easy to you to use C#.
First in C# the entry point of program is a static function called Main inside a class, not more than one.
If you want to compile one C# program then you can use the compiler:
cc yourprogram.cs
… or just use the Mono IDE.
C# has data types, operators, variables, constants, typecasts, branching like any programming language.
Also C# allow you to create your own data types. This mean the C# is an extensible language.
Let’s see more about this:

  • data types: bool,byte, sbyte, char, short, ushort, int, uint, float, long, ulong, double, decimal
  • operators: +,-,*,/,%,++,–,*=,/=,-=,%=,>>=,<<=, bitwise, logical, shifting operators …
  • variables: int my_var = 1;
  • constants: const float my_const = 5;
  • typecasts: C# need to know if is an explicit and implicit conversions.
  • branching: if, switch, while, for, do-while, …

After you open the Unity 3D IDE the next step is to create the C# script.
Right click with the mouse into Project tab, like in the next image and simply create the script.
unitycsharp_script
The default script is named NewBehaviourScript and come with this source code:

You can see the class NewBehaviourScript calls to the Unity Engine ( see: using UnityEngine).
Also two functions:
Start() – this is called when the scene first launches and Update() – This is called in every frame that the game runs.
Unity 3D will rewrite the class name in your C# script when you will rename your script in the Project panel.
You can create new scripts and also can be used to call different features of the game engine.
Foe example you can create one new script to move, rotate or scale one object from Unity 3D Engine.

Share