Unity 'Hello World'
Printing to console
-
Create new project (set
Templateto2D). -
Inside
Assetscreate scripthello.cswith the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hello : MonoBehaviour {
// Use this for initialization
void Start () {
print("Hello World");
}
// Update is called once per frame
void Update () {
}
}
That is:
- right click in
AssetsareaCreate > C# Script, - rename it
Hello.cs, - double click on it,
- add
print("Hello World");insideStartmethod, - save it and build it (just in case).
-
Attach script
hello.cstoSampleScene-Main Cameraby drag-and-drop the script ontoMain Camera. -
Save Scene and run (press
>button). -
Switch to
Consoletab in order to see something like
[13:29:19] Hello World
UnityEngine.MonoBehaviour:print(Object)
Printing to Scene
In order to print inside the scene you should add a method
void OnGUI()
{
GUILayout.Label("Hello World");
}
into class Hello. So the script Hello.cs should like like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hello : MonoBehaviour {
// Use this for initialization
void Start () {
print("Hello World");
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.Label("Hello World");
}
}
”