Unity 3D Game Creation Using C# Script

  

This article describes the following:

  • Introduction to Unity 3D.
  • Install Unity from Visual Studio 2015.
  • Create your first Unity 3D application.
  • Introduction to the Unity 3D Framework.
  • How to add a C# Script to your Unity 3D application.
  • How to add the C# Script to a selected object to produce the result.
  • How to build a Unity 3D application and run it in the web.

Introduction to Unity 3D

Unity supports development of game applications. Using Unity we can create 2D and 3D games. It also supports multiple platform, in other words we can build and deploy our Unity program to the web, Windows, MAC OS, iOS, Android, BlackBerry and so on. It's interesting, right? Yes, this is the main advantage of Unity since we write once and can deploy to multiple platforms. Another advantage of Unity is we can write our C# script to perform actions on Game Objects. For example, we can write a C# Script to rotate 3D text, Cube and so on. We can see more details of how to install Unity and how to work and run our first Unity Game application.

You can refer to the Unity3D website for more details.

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

For Unity C# script tutorials kindly refer to this link. You can find both a C# script API and a 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 here

After installing Visual Studio 2015 click Start, then Programs and click Visual Studio 2015.

Click New, then Project and select Game. If you didn't installed Unity in your computer you can install from here. Select Install Unity and click OK.

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

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

 
Once the Unity installation completes, click the Start Menu. Then go to the Unity Folder and click Unity.
 
 

When you click Unity from the 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. 
 


If you have a Professional Edition license then you can provide the license code otherwise select Unity Personal Edition and click Next.
 

Create your first Unity 3D application

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



Select your project folder and provide your Unity application name and select 2D or 3D. In this example I used Unity 3D so I selected 3D and clicked Create project.
 

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

Code Part

  • Introduction to Unity 3D Framework

    After creating a new project we can see the Unity Framework as in the following. In the Unity framework we can see 6 main parts:

  • Hierarchy Window

    All the added game objects, Camera and Light for our Scene will be listed in this window. We can add 2D and 3D objects and so on to our scene by right-clicking the Hierarchy Window and selecting depending on our needs. (Here are the game objects like Cube, Sphere3D text and so on.) You can also add the objects from the GameObject menu.
 
  • Project Window

    We can see our entire project file in this window. We can add a C# Script file from this window.

  • Scene Window

    We can see all our objects that have been added to the scene here. For example, if we add a Cube and 3D Text then we can see them in this Scene window also.

  • Game Window

    Here we can see the final output of our scene. We can click the run button to see the game output.

  • Console Window

    In this window we can see an error message, if any, that we have in our script file.

  • Property Window

    We can change the selected object Font color, Alignment and and so on.

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

  • Main Camera: This will be used to display our objects in whatever angle we placed it at.
 

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

  • Object Toolbar: We can see the following tool bar at the top of the Unity Framework.
    
  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 the Move tool and select some object from the scene and drag the object we can see the X and Y positions of the object will change from the property window.

  3. The third tool will be used to rotate the selected object. When we click on the rotate tool and select some object from the 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 change. 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 a Directional Light.

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

Now in the property window we can see 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 that was initially White in color due to the light placed for the Capsule Object but it has a Green color light effect on it.




3D Text Property

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



How to add a C# Script to a Unity 3D application

So far we have added our basic design to the 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 Windows Forms application we can see a code-behind (.cs) file to add the C# code part but in Unity we can add C# code by adding a C# Script from the Project Window.


To add the C# script file right-click on the project window as in the following and select Create, then click on C# Script.




Then we will add the C# Script file. Here we can see that the C# file is added. We can rename the C# Script file depending on our needs. For example, here I named the C# Script file “shanuBehaviour”.



To write our C# code on the Script file, double-click on the C# Script file that we added.

We can see the C# Script file will be opened in the Mono Editor as in the following screenshot.

MonoDevelop is an IDE that is the default editor for writing the scripts such as C#, JavaScript and and so on. For more details on MonoDevelop refer to this link.

  

C# Script file Code Part: By default when we open the C# Code file it will look as in the following code.

We can see 2 default namespaces will be added to our C# Script file.

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

When we create our C# script file we can see the 2 default method Start() and Update().

  • Start() Method: The Start() method is similar to our Webform PageLoad or Windows Forms FormLoad event. The Start method will be run only once at the start of the program.

  • Update() Method: The Update() method is an important method since this method will be called every time to perform our action. For example, if we need to rotate an object then 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 continous rotation of the object from our game output. 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class shanuBehaviour : MonoBehaviour {  
  5.   
  6.     // Use this for initialization  
  7.     void Start () {  
  8.       
  9.     }  
  10.       
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.             }  
  14. }  
Simple C# Script file to rotate our Objects

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


The transofrm.RotateAround()
method will be used to rotate the objects. We pass the parameters as rotate directions and time interval to the RotateAround() method. 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class shanuBehaviour : MonoBehaviour {  
  5.   
  6.     // Use this for initialization  
  7.     void Start () {  
  8.   
  9.     }  
  10.       
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.         transform.RotateAround (Vector3.right,Vector3.up , 40 * Time.deltaTime);  
  14.     }  
  15. }  
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.
 
 
 
Console Window: We can see the error messages of our C# Script from the 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 the Game window we can see the error message as in the following screenshot:
 
 
 
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 the Hierarchy window. See the following GIF image to see how to add the C# Script file to an Object.

 

Now 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 a browser, click on File -> Build and Run.



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

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

Run your HTML file to see our game output in the browser. We need to install the Unity Web Player to view the game in the browser.

Up Next
    Ebook Download
    View all
    Learn
    View all