Coding in Unity for the Absolute Beginner

Checked with version: 4.6

-

Difficulty: Beginner

In this session we will introduce coding in C# to the absolute beginner. This class is for someone who wants to get started, but doesn't know where to begin. We will discuss the very basics of coding, including variables, functions and classes and how to use them. We will also discuss using the most common of Unity's built in functions and when to use them and when to write our own. When this session is finished, we will be able to start the introductory project Roll-a-ball, and then onto more advanced projects like "Space Shooter" and "Nightmares". Tutor - Adam Buckner

Coding in Unity for the Absolute Beginner

Beginner 2D Game Creation

DemoScript [Revision 1]

Code snippet

using UnityEngine;
using System.Collections;

public class DemoScript : MonoBehaviour {

    public Light myLight;

    void Update () {
        if (Input.GetKey ("space")) {
            myLight.enabled = true;
        } else {
            myLight.enabled = false;
        }
    }
}

DemoScript [Revision 2]

Code snippet

using UnityEngine;
using System.Collections;

public class DemoScript : MonoBehaviour {

    public Light myLight;

    void Update () {
        if (Input.GetKeyDown ("space")) {
            myLight.enabled = true;
        }

        if (Input.GetKeyUp ("space")) {
            myLight.enabled = false;
        }
    }
}

DemoScript [Revision 3]

Code snippet

using UnityEngine;
using System.Collections;

public class DemoScript : MonoBehaviour {
    
    public Light myLight;
    
    void Update () {
        if (Input.GetKeyDown ("space")) {
            myLight.enabled = !myLight.enabled;
        }
    }
}

DemoScript [Revision 4]

Code snippet

using UnityEngine;
using System.Collections;

public class DemoScript : MonoBehaviour {

    public Light myLight;

    void Update () {
        if (Input.GetKeyDown ("space")) {
            MyFunction ();
        }
    }

    void MyFunction () {
        myLight.enabled = !myLight.enabled;
    }
}

DemoScript [Revision 5]

Code snippet

using UnityEngine;
using System.Collections;

public class DemoScript : MonoBehaviour {

    public Light myLight;

    void Awake () {
        int myVar = AddTwo(9,2);
        Debug.Log(myVar);
    }

    void Update () {
        if (Input.GetKeyDown ("space")) {
            MyFunction ();
        }
    }

    void MyFunction () {
        myLight.enabled = !myLight.enabled;
    }

    string AddTwo (int var1, int var2) {
        int returnValue = var1 + var2;
        return returnValue;
    }
}

DemoScript [Revision 6]

Code snippet

using UnityEngine;
using System.Collections;

[System.Serializable]
public class DataClass {
    public int myInt;
    public float myFloat;
}

public class DemoScript : MonoBehaviour {

    public Light myLight;
    public DataClass[] myClass;

    void Awake () {
        int myVar = AddTwo(9,2);
        Debug.Log(myVar);
    }

    void Update () {
        if (Input.GetKeyDown ("space")) {
            MyFunction ();
        }

        rigidbody.velocity = 10.0f;
    }

    void MyFunction () {
        myLight.enabled = !myLight.enabled;
    }
    
    string AddTwo (int var1, int var2) {
        int returnValue = var1 + var2;
        return returnValue;
    }
}