You are here:   Blog
Register   |  Login

Unity 5 Hello World!

Oct 12

Written by:
10/12/2015 9:07 PM  RssIcon

image

The common way to learn a new language or a programming framework is to start with a simple “Hello World!” tutorial…

image

The first step is to download and install Unity 5.

image

Open it.

image

Select Projects then NEW.

image

Call the project HelloWorldProject, select 2D and click the Create project button.

image

When it opens, ensure that you have Default layout selected.

image

From the menu bar, select GameObject then UI then Text.

image

A Canvas with a Text object and an EventSystem will appear.

image

Hold the Alt and right click-drag to zoom out (or click in the scene and scroll out using your mouse wheel).

Zoom out until you can see the Text Box.

image

Click on the Text Box and drag it until it is on the Canvas.

image

Ensure that the Rect tool is selected.

image

Select the Text Box, and in the Text section of its properties (in the Inspector), enter the words “Hello World!”.

image

Click the Play button.

The text will display.

image

Click the Play button again to stop the program and return to design mode.

Programmatically Create Hello World!

image

Select the Rect tool, select the Text Box, and in its properties (in the Inspector), select Add Component.

image

Scroll down to the bottom of the list and select the arrow next to New Script.

image

Enter TextController for the Name, ensure C Sharp is selected for the language, and click the Create and Add button.

image

The TextController script will be created and display in the Assets folder.

The script will also be attached to the TextBox.

Double-click on the TextController script in the Assets folder to open it.

image

The script editor will open.

image

 

Change all the code to the following:

 

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextController : MonoBehaviour
{
    public Text TextBoxText;
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        TextBoxText.text = "Hello World! (From Code)";
    }
}

 

image

Save the code.

image

Switch back to the Unity editor and click on the Text Box to select it.

Drag the Text from the Hierarchy to the box next to Text Box Text property in the  Text Controller that is attached to the TextBox.

(This sets the Text Box as the TextBoxText property in the script we created. That script will set the text of the Text Box)

image

Run the scene and see the result.

Save The Scene

image

From the menu, select File then Save Scene as…

image

Enter Main for the File name and click Save.

image

The scene will save in the Assets folder.

Double-clicking on a scene in the Assets folder will open that scene.

Links

Beginner Tutorial: A Unity 5 Number Guessing Game

Download

You can download the code from the Download page.

Unity 5 (or higher) is required to run the sample code.


Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel