Home > Tutorials > Gaming > Coding C# in Unity for Beginners

Coding C# in Unity for Beginners

Using Unity as a game engine to make 3D and 2D games scripted in C# can be very rewarding. Accomplishing that task, however, can be challenging for many.

In the video tutorial above I demonstrate how to go from zero experience in coding to being able to find your way around the Unity game engine, place objects (GameObjects in Unity) into the scene and then create simple scripts to bring your project to life.

The video should also help experienced coders that are new to the Unity platform by showing where all of the primary Unity features are located.

Towards the bottom of this post I will be inserting a transcript of the video for those that learn better from reading.

As part of the video, we create a C# script to use on our objects. The script used is below.

Script Used in the Unity Video Tutorial:

Remember that the script filename and the class name must be the same (MyColorScript for my example).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyColorScript : MonoBehaviour
{

    public GameObject plane;
    private MeshRenderer myRenderer;

    private void Start()
    {
        Debug.Log("We made it here");
    }

    public void OnClickChangeColor ()
    {
        myRenderer = plane.GetComponent<MeshRenderer>();
        myRenderer.enabled = !myRenderer.enabled;
    }

}

Top Tips for Unity Beginners

  1. Be consistent and patient – just the way coding is.
  2. Complete online tutorials and watch C# videos to learn new skills.
  3. Keep your code organized and add comments to remember things.
  4. Practice following your code using Debug.Log(); and you will eventually find the problem.
  5. Don’t get too caught up with store assets right away, they can be difficult to integrate for less experienced coders.

If you’re looking to hone your skills, there are some great Unity tutorials on the Unity3D website as well as on YouTube.

Unity Tutorial for Beginners (Text Version):

Intro

Hello everything this is Anson from AnsonAlex.com and in this video I’m going to show you everything you need to know to get up and running with Unity for creating games and for coding object oriented projects using C#.

If you’re brand new to coding and game design in general, it’s my hope that you’ll be able to find yourself around Unity and start putting your first project together after watching. If you’re an experienced developer new to Unity, you should be able to get started coding right away after I help you get oriented to Unity.

As always with my videos it’s best to follow along on your computer pausing and rewinding when necessary – I do keep the pace up in order to cover so much information.

Unity is available for Mac, Windows, Linux and a number of other platforms.

Unity Installation

For the purpose of this tutorial I’m going to assume that you’ve already gone to unity3d.com, downloaded the program for your operating system and have it installed.  It’s a good idea to install any recommended libraries that Unity suggests during the installation.

You’ll also need an IDE like monodevelop or visual studio – I believe Unity prompts you to install Visual Studio at some point during the installation process so that’s a good idea if you’re unsure of which IDE you’d like to use.

Creating a New Unity Project

Okay, with that out of the way, let’s take a look.  As you can see, I have Unity open on my computer and, although I could open up a project I’ve already worked on from here, I’m instead going to click on the New button up here at the top to begin a new project from scratch.

In the window that appears, I can specify some of the details about my project. I’ll just call it Test Project and then I can select a template.  The template selection doesn’t really matter to be honest, we can change this setting at any point in the future but we’ll just select 3D for now.

Then choose an appropriate folder on your computer to keep your project stored.  Also, if you’re new to developing, I highly recommend that you check out Github.com to backup and help organize your project.

Okay, great, now let’s click Create Project.

Unity User Interface Introduction

So here we are in Unity.  Let’s take a look around the user interface a bit and, as we do so, we’ll start using Unity tools to create a simple project.

First of all, there are many different layouts that you can use when working in Unity.  This is the default layout where, up here at the top left we have 3 tabs – our Scene, which is where we are now and where we will be placing objects, known as GameObjects in Unity, into our project so that we can interact with and script them.  The next tab is the Game tab where we will debug and test our game and then we have the Unity asset store where we can buy all sorts of assets for our projects.

As you can see, I can drag and move these windows around how I would like.  I can even move them to the complete other side of the application. This goes for any of the windows that we work with here in Unity.

Notice that right below these three tabs I also have the option to switch back in forth between 2D mode and 3D mode.  I told you it was easy!

Working in the Unity Hierarchy Window

Now let’s move our focus over to the right, this is the hierarchy window which will serve as a list view of all of the GameObjects in our Unity project.  As you can see, by default, a new project comes with two GameObjects, a main camera and a directional light – (without these two objects, both a light and a camera, game view would just appear as a pitch black nothingness).

If I click on one of the GameObjects already in our project, notice that information regarding that object appears to the right in the Inspector window.  This is where we can modify the properties of our objects and components here in Unity. Notice that, with the camera GameObject selected in our Hierarchy, we see that it currently has 3 components attached to it.

The Transform component which every object in Unity has, even if it’s just a placeholder object.  The transform component essentially tells us where the object is located in our Scene. We also have a camera component attached to our camera gameobjects, makes sense. And then, if we scroll down, we also see the Audio Listener component which I’m not going to cover in this video.  

Adding Components to GameObjects

We can also click on this “Add Component” button to add additional components to the selected object. As you can see, there are a number of different components.  Play around with them if you’d like but, if you’re brand new to coding, it’s probably best to discover a lot of these components by following tutorials and searching for information on them when needed.

We’ll come back here in a little bit but, for now, let’s move on to the bottom of our screen.

Down here notice that we have two different, both very important, tabs.  The first one is the Console which is where we will see any errors related to our builds and where we can use monobehaviour’s built in Debug.Log method to write debugging information to.

We’ll look at this later on when we start scripting as well.

The second tab is the project tab where all of the game files such as scripts, prefabs, materials, textures, animations and so forth are stored – on your computer, this is located at the file path that you specified when you created the project.

So notice that, with the asset folder selected, we already have one folder created in here for us.  This is scenes folder that is created by default and, inside of it, we have our default scene called SampleScene.  This is the scene that we’re looking at in the “Scene” view above and what we would see if we clicked on the Game tab.

Unity projects often consist of many scenes but don’t have to.  You can create a fully functional game with just one scene so, if you’re new, I wouldn’t worry about creating any extras just yet.

Creating Folders in Project Window

I do, however, have something that you should be worrying about.  And that is – how to organize all of the game files related to your project so let’s create a few folders to help us do that.

So I’m going to right click here in the assets folder and, notice, we have a ton of different options… for now, however, I’m just going to go to Create and then Folder.

Let’s name this folder Scripts – you can name it anything you want but scripts is generally considered the industry standard.  This will be one of the most important folders in our project – where we keep all of our C# scripts.

Also in the assets folder it is a good idea to create a folder called Prefabs, a folder called Materials, a folder called Textures and one called Sounds.  This is of course up to you, you’re the developer and as you get going on your project you’ll probably need to create more folders specific to your situation but this is a good place to start.

Okay great – so now that we’ve taken a look around Unity let’s add some items to our scene and write a simple script for them.

Adding Objects to the Scene

To add a new object to the Scene I can right click in the hierarchy view or I can click on the GameObject menu at the top of the screen.  As you can see, there are a number of different objects I can add – we’re going to add a 3d plane to our scene but we could add another camera, more lighting or user interface objects from here.

But let’s select a 3d plane for now.

As you can see, the plane now appears in my scene view.  I can modify some of the physical properties of the plane using the different selectors up here at the top left.  The one that I currently have selected will allow me to move the plane around the scene.

Adjusting Objects in the Scene

Notice that, as I move it around, we can see the numerical properties of the plane change in the inspector over on the right – specifically the X Y and Z axis.  The hand selector simply allows you to navigate around your scene and view things from different angles.

Left clicking and moving allows you to move around in the scene and right clicking and moving allows you to rotate your view.

The third selector deals with object rotation, the 4th selector object scale. The 5th scale and position and the 6th position and rotation.

You can also use the hierarchy window to adjust these properties.  So if I change the X position to 100, you can see it moves out of my view.  I also want to just confirm with everyone that in Unity, as you would expect, X deals with moving what we would consider left and right, Z deals with moving forward and backward and Y deals with up and down.

Also, it’s a good habit to always reset a new gameobject to 0,0,0 in the world to avoid confusion when placing where you want it.  To do this we can either enter 0,0,0 in the X Y and Z coordinates or we can just right click on the transform component and click “reset”.

Entering Play Mode in Unity

Okay, so now we have this plane in our Scene and if we hit the play button up here at the top we’re brought into game mode and we can see that our plane exists.

It’s important that when you are here in the game view, that you set the resolution up at the top to the resolution that you plan on developing for.  That way, in the scene view and in the game view, your project will appear properly.

That being said, our plane is here but nothing is happening right now, so let’s go back into the editor for now and look at some more things.  I’m going to click on the play button again to exit game mode.

So basically, in order to show and UI element in this video and to show you some scripting.  We are going to give this plane a material and a color and then we’re going to create a button and write a script that will enable and disable the renderer on our plane – meaning we can change its color by clicking the button.

Creating a New Material in Unity

Okay so, first let’s add a color to our plane.  To add a color we need to create a new material.  We can do that by going to our assets folder in the project view, then into the materials folder that we created and then we can right click and click create new material.

We’ll name this material blue and then notice that it’s properties appear in the inspector to the right.  So we can go in here and select the color picker and turn this material blue.

Now, all we have to do is drag the blue material from the materials folder on to the plane in our scene.  As you can see our plane is now blue. If we click on the plane, we can see in the inspector that the blue material is now attached as the shader of this object.  This component, the shader, is actually part of the Mesh Renderer component which we will be referencing in our C# script.

Okay great, we’re almost ready to create a script for this project but, first, we need some UI elements so that we can interact with our script.

Creating Unity UI Elements

So let’s go back over to our hierarchy, right click, go to ui and click canvas.

When creating a user interface in Unity you will almost always create everything under a canvas.  So notice that, now, in my scene view I have the canvas GameObject. To view it in the scene I can just double click on in the hierarchy.

As you can see we’re brought to the canvas and it just look like an outline of a box floating floating in space.  We can move this UI around anywhere in the Scene that we want but as long as we keep the “Screen Space Overlay” option selected over here in the inspector, it will appear as an overlay on our players screen when they enter the game.  So we could create a detailed UI just with Unity’s built in tools but all we really need is a button so lets right click on the canvas in the hierarchy and create a UI element – button.

Notice that, because I did this from right clicking on my canvas, my button as created as a child object of my canvas.  This means that the 0,0,0 position of my button is relative to the canvas, not to the rest of the world.

Okay great and if we click play, we can see our plane and our button in Game mode.

Still nothing happening though, it’s finally time to create our script.

Creating C# Scripts in Unity

So lets move into the scripts folder in our project view, right click, go to create and then click C# script.

Naming your scripts is really important.  Make sure that it starts with a capital letter and don’t change the name later on unless you know what you’re doing.  When in doubt, if you just created a new script and messed something up, just delete it and create a new one.

Okay, we’ll call this one MyColorScript for now and then hit enter.

Now, in order for this script to be read in our project, it needs to be attached to a gameobject.  So let’s click and drag it into our hierarchy and attach it to our plane gameobject.

Now, if I click on the plane object, we can see that the script is attached.  Time to code. Double click on the script in the project view to open it up in your IDE.

Anson Alexander

I am an author, digital educator and content marketer. I record, edit, and publish content for AnsonAlex.com, provide technical and business services to clients and am an avid self-learner. I have also authored several digital marketing and business courses for LinkedIn Learning (previously Lynda.com).

View all posts