Do you want to customize the behavior and appearance of your game objects? You can use script to do just that. This tutorial will be the first of a series in which you will learn how to code a C# script for Unity. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!
Setting up the Scene
Create a new project in Unity named “CodeIntroduction”. Create a cube in the Hierarchy. Give Cube the position 0 0 0 so that it is centered in the Scene.
Let’s change Cube’s color. Create a material named “ColorMaterial” in the Assets folder. In the Inspector, change the material’s color to an orange with RGB values 155 153 0.
Drag and drop ColorMaterial from Assets to Cube in the Scene to apply the color to the cube. Set Main Camera’s Z position to -4 so that it is closer to Cube.
Save the Scene as “Game.unity”. A file named “Game” will appear in the Assets folder.
Creating the Script
Every game object in the Hierarchy has components in the Inspector. Unity provides components such as materials, collisions, and 3D primitives. To create a more complex game, you can write custom behavior for an object. To this, you create a script and attach the script as a component of the game object.
In the Project window, let’s make a script for Cube. Right-click in Assets. Select Create > C# Script.
A new file will appear in Assets. Name the file “Cube”.
When you create or update a script, a wheel will spin in the bottom right corner of the Unity window. This wheel spins when a compilation occurs. A compilation takes lines of code and converts it to a format that is easier for your game to process.
When the compilation is complete, the script has been added or updated. Then you can run the game to see the changes the script applies.
Every time you change and save a script, Unity will re-compile the project to ensure that everything is correct. If there is a mistake in your code, such as a missing character or invalid variable name, a message will appear in the Console.
Currently, the Cube script will not execute any code because it is not attached to any object. Let’s attach the script to the Cube game object. Drag and drop the Cube script to the Cube game object in the Hierarchy or Scene. Alternatively, drag and drop the script to a blank space in the Cube object’s Inspector.
A Cube (Script) component will appear in Cube’s Inspector.
Double-click on the Cube script in Assets to open it. The file “Cube.cs” will open in MonoDevelop-Unity, as evident in the next image. MonoDevelop is a free, open-source coding interface that downloads when you download Unity if you do not unselect Unity’s default downloads.
Note that if you prefer to use other tools, such as Visual Studio, Sublime Text, or Notepad++, you can configure Unity to use that text editor by default.
The first line in the script is
using UnityEngine;. This line means that the code in the script can use items already implemented in the Unity engine.
using System.Collections; means that the script can use certain parts of the code that are already written in the C# environment for collecting objects.
The other code in the file defines the Cube class.
public class Cube defines a public class called Cube.
: MonoBehaviour means the class extends MonoBehaviour, which is the base class for a game object. The code we write between the curly braces will change how Cube behaves.
The default methods in the Cube class are
Start method executes code when the game begins. The
Update method executes code constantly. Want to learn more about methods? Check out our Unity3D course, where you build 30 virtual reality games! Don’t forget to save your project.
- Writer of blog posts and books at Mammoth Interactive. Can also be found posting to Mammoth Interactive's social media.