Click here to Skip to main content
15,937,127 members
Articles / Game Development / Unity

Unity 3D Game Creation using C# Script

Rate me:
Please Sign up or sign in to vote.
4.94/5 (30 votes)
11 Sep 2015CPOL9 min read 78.7K   1.3K   37   26
In this article, we can see how to create a simple Unity 3D game using C# Script.

Image 1


This article covers the following topics:

  • Introduction to Unity 3D
  • Install Unity from Visual Studio 2015
  • Create your first Unity 3D Application
  • Introduction to Unity 3D Framework
  • How to add C# Script to your Unity 3D Application
  • How to add the C# Script to selected object to produce the result
  • How to Build and Run the Unity 3D Application to web

Introduction to Unity 3D

Unity is used to develop a game application. Using Unity, we can create 2D and 3D games. It also supports multi platform which means we can build and deploy our Unity program to WEB, Windows and MAC OS, iOS, Android, BlackBerry, etc. It’s interesting right, yes this is the main advantage of Unity as we write once and can deploy to multiple platforms. Another advantage of Unity is that we can write our C# script to perform some actions on Game Objects, for example, we can write a C# script to rotate 3D text, Cube, etc. We can see more details of how to install Unity and how to work and run our first Unity Game application.

You can refer the Unity3d web site for more details here.

For game lovers who don’t know how to write your game application using C#, it’s a very good place to start with Unity. Unity is a very good platform to start and write your own first game application and write scripts using C#.

For Unity C# script tutorials, kindly refer to this link. You can find both C# Script API and JavaScript API .

Install Unity from Visual Studio 2015

Here, I have used Visual Studio 2015. You can download the Visual Studio 2015 Community Edition from this link.

After installing Visual Studio 2015, click Start -> Programs -> Click Visual Studio 2015.

Click New -> Project - > Select Game. If you didn’t install Unity in your computer, you can install from here. Select Install Unity and click OK.

Image 2

Click Install to continue installation of Unity and Visual Studio Tools for Unity.

Image 3

Select both Unity and Visual Studio Tools for Unity and click Next which will install Unity in your computer. Wait for a few minutes to complete the installation process.

Image 4

Once the Unity installation is complete, click Start Menu -> Click Unity Folder and click Unity.

Image 5

When you click Unity from Start menu, for the first time, it will ask you to sign in using your Unity account. If you don’t have the Unity Account, you can create a new Unity account and use the account login to open your Unity framework.

Image 6

If you have Processional Edition License, you can give the license code, otherwise select Unity Personal Edition and click Next.

Image 7

Create Your First Unity 3D Application

Click Create new project to create your first Unity Game application.

Image 8

Select your Project Folder and give your Unity application name and select 2D or 3D. In this example, I have used Unity 3D so I have selected 3D and click Create Project.

Image 9

Now you are ready to work with your first Unity Game application development.

Using the Code

Introduction to Unity 3D Framework

After creating a new project, we can see Unity Framework as below. In Unity framework, we can see six main parts as below:

Image 10

  1. Hierarchy Window – All the added game objects, Camera and Light for our Scene will be listed in this window. We can add 2D, 3D Object, etc. to our Scene by right clicking the Hierarchy Window and select as per our need. (Here, the game objects like Cube, Sphere3D text, etc).You can also add the objects from GameObject menu.

    Image 11

  2. Project Window – We can see our entire project file in this window. We can add C# Script file from this window.
  3. Scene Window – We can see all our Objects which has added to the scene here for example if we add Cube and 3D Text, we can see in this Scene window as well.
  4. Game Window – Here, we can see the final output of our scene. We can click the Run button to see the game output.
  5. Console Window – In this window, we can see error message if any we have in our script file.
  6. Property Window – We can change the selected object Font color, Alignment, etc.

When we care our Unity program in Hierarchy window, we can see by default Main Camera and Directional Light will be added.

Main Camera

This will be used to display our objects in any angle as we placed.

Image 12

Here, we can see that I have added 3D Capsule object and 3D Text object to the scene. Now, we can see how to use property window for each selected object.

Object Toolbar

We can see the below toolbar at the top of Unity Framework.

Image 13

  1. The first tool will be used to Drag and adjust the Scene of our Game.
  2. The second tool will be used to move the selected object and place it in our desired location. When we click on Move tool and select some object from the scene and drag the object, we can see the X and Y position of object will be changing from the property window.
  3. The third tool will be used to rotate the selected object. When we click on rotate tool and select some object from scene, by clicking on the object, we can rotate the object as we needed, we can also see the selected object rotation X and Y value will be changing. To see the actual output, click on the Game window.
  4. The fourth tool is to Scale the selected Object. We can resize the selected object by clicking the Scale tool.

Property Window

Now we start with adding color and Directional Light.

The Directional Light object will be used to add light effect to our objects, for example, here we can see I have selected the Directional light and drag it and place it near the Capsule Object.

Now in property window, we can see that I have selected the green color for the Directional Light Object and I have increased the Identity of the Light Object from the property window.

We can see the final light effect on the Capsule object which was initially white in color due to light placed for the Capsule Object, now it has green color light effect on it.

Image 14

3D Text Property

Here, we can see similar to our Visual Studio Property window, the 3D Text object has properties like Text (we can add any text to the selected 3D Text object, for example, I have given my name as “Shanu”). We can add color to the Text property, etc. We can give name for object from the top of the Property window or from the Hierarchy window, we can rename our object names.

Image 15

How to Add C# Script to Your Unity 3D Application

So far, we have added our basic design to our Game application, now let’s see how to add some basic actions like rotating our 3D Capsule and 3D Text Object by writing a simple C# Script.

In our .NET Web or Winform application, we can see Code-Behind (.CS) file to add C# code part, but in Unity, we can add C# code by adding C# Script from the Project Window.

To add the C# Script file, right click on the Project window as below and select Create and click on C# Script.

Image 16

After we add the C# Script file, we can see now C# File will be added .We can rename the C# Script file as per our need for example, here I have named the C# Script file as “shanuBehaviour”.

Image 17

To write our C# code on the Script file, double click on the C# Script file which we have added. We can see the C# Script file will be open in Mono Editor as below.

MonoDevelop is a IDE which is default editor for writing the scripts like C#, JavaScript, etc. For more details on MonoDevelop, refer to the link here.

Image 18

C# Script file Code Part

By default, when we open the C# code file, it will look like the below code.

We can see that two default namespaces will be added in our C# Script file.

Our entire C # script class will inherit MonoBehaviour class by default. MonoBehaviour is the base class of all the script derives from. For more details of MonoBehaviour, kindly refer to this link.

When we create our C# Script file, we can see two default methods as Start() and Update().

  • Start() method: The Start() method is similar to our Webform Page Load or Winform Form Load Method. The Start method will be run only once at the start of the program.
  • Update() method: The Update() method is the important method as this method will be called every time to perform our action. For example, if we need to rotate an Object, we write code in the update method and assign the C# Script file to the Object. When we run the Unity program, every time this update method will be called and we can see continuous rotation of the object from our game output.
using UnityEngine;
using System.Collections;

public class shanuBehaviour : MonoBehaviour {

           // Use this for initialization
           void Start () {     
           // Update is called once per frame
           void Update () {

Simple C# Script File to Rotate Our Objects

Here, we can see that I have written a simple code on Update() method to rotate the objects in set of interval times.

transofrm.RotateAround() method will be used to rotate the objects. We pass the parameters as rotate directions and Time interval to the RoteAround() method.

using UnityEngine;
using System.Collections;

public class shanuBehaviour : MonoBehaviour {

           // Use this for initialization
           void Start () {
           // Update is called once per frame
           void Update () {
                     transform.RotateAround (Vector3.right,Vector3.up , 40 * Time.deltaTime);

After writing the C# code, save it in MonoDevelop and we can see the changes in our Unity framework at the right side of the property window.

Image 19

Console Window: We can see the error messages of our C# Script from Console Window. For example, I have misspelled the “this” keyword with “thisd”, we can see the error message at the bottom of the Console window and also when we click on Game window, we can see the error message as below:

Image 20

How to Add the C# Script to Selected Object to Produce the Result

To add our C# Script file to our selected object, click on the C# Script file and drag it to our selected objects in Hierarchy window. See the below Gif image to see how to add the C# Script file to our Object.

Image 21

Now that we have created our simple Game Application using Unity, run the program and check the output from the Game Window.

How to Build and Run the Unity 3d Application to Web

To build and run the output in Browser, click on File -> Build & Run.

Image 22

Select your Scene as we have created only one Scene, I will click on the Add Current button which will add our Current Scene to be build and run. In platform, we can see Web Player, PC, iOS, Android, etc. We can select any one to see our output in our selected platform. Here, I have selected the Web Player.

Image 23

If we click on Build, we can see the result output files will be added to our selected folder. Here, when we click on BuildOutput.html file, we can see our Unity game will be displayed in browser.

Image 24

We can install the Unity web player to see the output.

Image 25


  • 5th August, 2015: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

GeneralInteresting Pin
yuzaihuan23-Sep-15 17:04
yuzaihuan23-Sep-15 17:04 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA16-Sep-15 7:46
professionalȘtefan-Mihai MOGA16-Sep-15 7:46 
GeneralRe: My vote of 5 Pin
syed shanu16-Sep-15 14:14
professionalsyed shanu16-Sep-15 14:14 
GeneralMy vote of 5 Pin
moizsharp15-Sep-15 6:25
moizsharp15-Sep-15 6:25 
GeneralExcellent Article +5 Pin
Sheepings27-Aug-15 14:11
professionalSheepings27-Aug-15 14:11 
GeneralRe: Excellent Article +5 Pin
syed shanu27-Aug-15 14:50
professionalsyed shanu27-Aug-15 14:50 
GeneralRe: Excellent Article +5 Pin
Sheepings27-Aug-15 15:02
professionalSheepings27-Aug-15 15:02 
GeneralRe: Excellent Article +5 Pin
syed shanu27-Aug-15 16:28
professionalsyed shanu27-Aug-15 16:28 
GeneralMy vote of 5 Pin
BytePower25-Aug-15 7:33
BytePower25-Aug-15 7:33 
GeneralRe: My vote of 5 Pin
syed shanu25-Aug-15 13:07
professionalsyed shanu25-Aug-15 13:07 
Questionwhy install unity from vs2015.. Pin
andrea contoli23-Aug-15 20:51
andrea contoli23-Aug-15 20:51 
AnswerRe: why install unity from vs2015.. Pin
syed shanu23-Aug-15 21:56
professionalsyed shanu23-Aug-15 21:56 
GeneralRe: why install unity from vs2015.. Pin
Sheepings27-Aug-15 14:46
professionalSheepings27-Aug-15 14:46 
Questionwow interesting Pin
georr11-Aug-15 22:16
georr11-Aug-15 22:16 
AnswerRe: wow interesting Pin
syed shanu11-Aug-15 22:28
professionalsyed shanu11-Aug-15 22:28 
Questiongreat article Pin
OwenGrad11-Aug-15 19:48
OwenGrad11-Aug-15 19:48 
AnswerRe: great article Pin
syed shanu11-Aug-15 19:58
professionalsyed shanu11-Aug-15 19:58 
GeneralMy vote of 5 Pin
Santhakumar Munuswamy @ Chennai8-Aug-15 5:06
professionalSanthakumar Munuswamy @ Chennai8-Aug-15 5:06 
GeneralRe: My vote of 5 Pin
syed shanu9-Aug-15 14:48
professionalsyed shanu9-Aug-15 14:48 
Questionvs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 21:36
professionalsreeyush sudhakaran5-Aug-15 21:36 
AnswerRe: vs2013 Compatible? Pin
syed shanu5-Aug-15 21:38
professionalsyed shanu5-Aug-15 21:38 
GeneralRe: vs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 22:19
professionalsreeyush sudhakaran5-Aug-15 22:19 
AnswerRe: vs2013 Compatible? Pin
Zephire5-Aug-15 21:43
Zephire5-Aug-15 21:43 
GeneralRe: vs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 22:17
professionalsreeyush sudhakaran5-Aug-15 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.