0
0
Unityframework~10 mins

First Unity project (Hello World scene)

Choose your learning style9 modes available
Introduction

Making your first Unity project helps you learn how to create and run a simple game scene. It shows you how to display text on the screen, like saying "Hello World" to start.

When you want to learn how to create a new Unity project.
When you want to display simple text in a game scene.
When you want to understand how Unity scenes and scripts work together.
When you want to test that your Unity setup is working correctly.
When you want to practice adding objects and scripts in Unity.
Syntax
Unity
1. Create a new Unity project.
2. In the Hierarchy, create a UI Text object: GameObject > UI > Text - TextMeshPro.
3. Create a new C# script and attach it to a GameObject.
4. In the script, use:

using TMPro;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    public TMP_Text helloText;

    void Start()
    {
        helloText.text = "Hello World!";
    }
}

You need to have TextMeshPro package installed (usually included by default).

Attach the script to any GameObject in the scene and link the TextMeshPro UI text in the Inspector.

Examples
This script changes the text of a TextMeshPro UI element to "Hello World!" when the scene starts.
Unity
using TMPro;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    public TMP_Text helloText;

    void Start()
    {
        helloText.text = "Hello World!";
    }
}
This uses the older UI Text component instead of TextMeshPro.
Unity
// Alternative: Using UnityEngine.UI.Text (older UI system)
using UnityEngine;
using UnityEngine.UI;

public class HelloWorld : MonoBehaviour
{
    public Text helloText;

    void Start()
    {
        helloText.text = "Hello World!";
    }
}
Sample Program

This complete example shows how to set up a simple Unity scene that displays "Hello World!" using a TextMeshPro UI text and a script.

Unity
// HelloWorld.cs
using TMPro;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    public TMP_Text helloText;

    void Start()
    {
        helloText.text = "Hello World!";
    }
}

// Steps to run:
// 1. Create a new Unity project.
// 2. Add a Canvas to the scene (if not present).
// 3. Add a TextMeshPro - Text object to the Canvas.
// 4. Create this HelloWorld script and attach it to an empty GameObject.
// 5. Drag the TextMeshPro text object to the 'helloText' field in the Inspector.
// 6. Press Play to see "Hello World!" on the screen.
OutputSuccess
Important Notes

Make sure to import TextMeshPro essentials if prompted by Unity.

UI elements like TextMeshPro need a Canvas in the scene to be visible.

You can customize the font size, color, and position of the text in the Inspector.

Summary

Creating a first Unity project helps you understand scenes, UI, and scripting basics.

Use a TextMeshPro UI text to show messages like "Hello World!" on screen.

Attach scripts to GameObjects and link UI elements to control what they show.