0% found this document useful (0 votes)
335 views

Learningcsharpforunity Helloworld

1) The document describes creating a basic "Hello World" program in Unity using C# to output text to the console. 2) The first attempt at running the program failed because scripts must be attached to a game object in the scene to execute. 3) A new empty game object called "Debug" was created to attach the example script to. 4) Running the program again after attaching the script successfully outputs "Hello World" to the console.

Uploaded by

api-297718659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
335 views

Learningcsharpforunity Helloworld

1) The document describes creating a basic "Hello World" program in Unity using C# to output text to the console. 2) The first attempt at running the program failed because scripts must be attached to a game object in the scene to execute. 3) A new empty game object called "Debug" was created to attach the example script to. 4) Running the program again after attaching the script successfully outputs "Hello World" to the console.

Uploaded by

api-297718659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Learning C# for Unity - Hello World

CCE VGD/GP

Hello, World!
Lesson Highlights
Creating a New Project in Unity
Creating a C# Script
Creating an Empty Object
Editing a C# Script
Attaching a Script to an Object
Executing a Program
Reading the Console

Required Software
Unity (There is a free version of Unity
available at: http://unity3d.com/getunity/download?ref=personal)
Code editor (MonoDevelop, Visual Studio,
Notepad++, etc.)

A "Hello, World!" program is a computer program that outputs "Hello, World!" (or
some variant thereof) on a display device. Because it is typically one of the simplest
programs possible in most programming languages, it is by tradition often used to
illustrate to beginners the most basic syntax of a programming language. It is also
used to verify that a language or system is operating correctly.1
In this exercise we will write our very own Hello, World! program using the Unity Game Engine.
Lets start off by creating a new Unity Project titled Learn C Sharp (Were avoiding the # symbol because as
a character in a folder/file/project name it can be rather problematic), setting the location, and clicking Create
Project.

Wikipedia,. '\'. N. p., 2015. Web. 11 Oct. 2015.

Learning C# for Unity - Hello World

CCE VGD/GP

Once Unity re-launches, we will be greeted with the Unity Editor, which looks like this:

We will get into what all is contained in the Unity Editor in another lesson. Whether or not you have finished the
Interface Tutorial, you should still be able to follow along with this tutorial.
Start off by Right-Clicking in the Assets pane, selecting Create, then C# Script.

Learning C# for Unity - Hello World

CCE VGD/GP

Name the new C# file ExampleClass

Once you have named the file, Right-Click and select Open, or simply Double Click (LMB) to open the file in
your code editor. For this tutorial, I am using Visual Studio Code, however the default is a program called
MonoDevelop. There is no difference between the basic usability and major function of these two programs; I
have simply chosen to use VSCode for visual appeal in this tutorial.
When your script loads, you will the following lines of code:

In the lesson titled Basic Script Elements, we will look at the individual elements of the script, however our goal
here is simply to execute a script, so thats what we will focus on.

Learning C# for Unity - Hello World

CCE VGD/GP

First, we need to trim the excess. Delete lines 10-14. We wont need these.

This will leave us with just the following 11 lines of code:

The simplest way to see if our script runs is to output to the Console in Unity. We can accomplish this easily by
using the command Debug.Log
More information on using Debug.Log can be found at: http://docs.unity3d.com/ScriptReference/Debug.Log.html

Learning C# for Unity - Hello World

CCE VGD/GP

The proper syntax for a basic Debug.Log command is as follows:

Once you have finished your script, make sure you save (CTRL-S on PC or CMD-S on Mac, or File > Save) any
changes you have made.
Now, back in the Unity Editor, click the Run/Play button up top.

Hmm Seems like nothing happened other than the preview window changing, and the color of the editor
changing.

So, what did we do wrong?


5

Learning C# for Unity - Hello World

CCE VGD/GP

Well, lets start off by pressing the Run/Play button again to stop running the scene (Think of this as a toggle
switch for Running/Not Running, not simply a Start button)
In order to execute a script, it must be attached to an object. We could attach the script to any object, including the
Main Camera or the Directional Light. This however is NOT a best practice, and we dont want to build bad habits,
so lets create an object to attach the script to.
In the Hierarchy on the left, click Create, then Create Empty

We now have a Game Object called GameObject in the Hierarchy.

This is a bit of a messy name. Give it a meaningful name. In the Inspector on the right, change GameObject to
Debug

Learning C# for Unity - Hello World

CCE VGD/GP

Now, to attach the script, you can either click-and-drag from the Project Tab > Assets to the Inspector, or
you can click Add Component in the Inspector, go to Scripts, and select Example Class, OR you can simply
search for the script in the top search box.

Now that the script is attached, lets re-run the program by clicking the Run/Play button up top.

Look down in the very bottom left corner of the Unity Editor.

Hello, World! Beautiful. Weve done it. To get a better view of the Console, simply click on the Console Tab
(Next to the Project Tab), and you will see the full output.
(For more information on the Console, visit http://docs.unity3d.com/Manual/Console.html)
7

Learning C# for Unity - Hello World

CCE VGD/GP

LESSON SUMMARY
Script: (QR leads to pastebin)

Review Questions
1.
2.
3.
4.
5.

Why didnt the program execute the first time we attempted to run it?
Where do you locate the output of Debug.Log?
What did you notice about the name of the public class (line 4)?
How could you use Debug.Log in the games you make?
What else does the Console tell you?

Challenge
Using the Unity API as your guide, change the appearance of your Debug.Log message (hint, Unity supports
<color> <b> <i> and <

size> tags).

Learning C# for Unity - Hello World by Levi Sterling is licensed under a Creative Commons Attribution 4.0
International License. This tutorial was written on Mac OSX, using Unity (Educational License), Visual Studio Code, and
Google Docs.

You might also like