Can I get an example of "Hello World" in Unity C#

Hi, I'm new to Unity 3D, My background is in Flash Actionscript3, so when I have been looking around for tutorials, it feels a bit like old skool flash where you attache snippets of code to visual objects on the screen. So my questions are.

  1. Is it possible to have something like a document class, a piece of code that runs first, where I can instantiate objects. Place things on screen etc. etc. As supposed to dragging and dropping things and placing things visually?

  2. C# seems neat so that's he most tempting way to go. I haven't been able to find any "getting started" tutorials. I would like something like HelloWorld, and maybe something where you get to set up the environment and throw a few cubes on the screen and get them to spin using code.

Thanks in advance /Philip

1) No, you have to have at least one script attached to a GameObject in your scene. This can be an empty gameobject (i.e. a non-visible item), but there's no scene root node, so you need this.

The script's "Awake" or "Start" functions can then be used to create and trigger other classes and functions. There's no "scene root node".

2) Here's a few of c# examples to make an object say hello, move, rotate, or be clickable in the scene:

// HelloWorld.cs    (outputs a line of text to the console)
using UnityEngine;
public class HelloWorld : MonoBehaviour {

    void Start() {
        Debug.Log("Hello World!");
    }
}


// RotateMe.cs    (rotates the object at a specified speed)
using UnityEngine;
public class RotateMe : MonoBehaviour {

    public float rotateSpeed = 10;
    void Update() {
        transform.Rotate( rotateSpeed * Time.deltaTime, 0, 0 );
    }
}


// MoveMe.cs  (moves the object via the arrow keys)  
using UnityEngine;

public class MoveMe : MonoBehaviour {

    public float moveSpeed = 2;

    void Update() {
        float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
        float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Translate( moveX, 0, moveZ );
    }
}


// ClickHello.cs (outputs a line of text to the console when the object is clicked)
using UnityEngine;
public class ClickHello : MonoBehaviour {

    void OnMouseUp() {
        Debug.Log("Hello World! "+name+" was clicked!");
    }
}

These scripts show a few fundamental aspects of coding in Unity:

  • Public variables are exposed in the inspector

    This means you can adjust the variable’s value for each object independently which uses this script, using Unity’s inspector window

  • Framerate-indepenedent movement using Time.deltaTime This allows you to move an object at a constant speed regardless of the framerate currently being achieved by the computer. This avoids problems such as cars driving fater on higher-spec machines!

  • The Start and Update functions These are examples of built-in unity events which are sent by the engine. Start is called when an object comes into existence, and Update is called every frame. There are many other event functions which are sent to any scripts attached to Game Objects. For the full list, see the MonoBehaviour reference.

  • Direct references to common components, such as transform Because your script inherits from MonoBehaviour (by default), you inherit these properties which are useful aliases to common types of component that get attached to gameobjects such as "transform", "collider", "renderer" "light", "camera", etc. For the full list, see the Component reference.

  • Input class

    contains the API for all types of input including keypresses, buttons, custom axes, iphone touches, etc.

For more general information about getting started learning unity, see:

How can I start learning Unity fast?

Good luck!