Friday, September 9, 2016

CODING IN UNITY FOR THE ABSOLUTE 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

DemoScript  C#

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;
        }
    }
}
-------------------



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;
        }
    }
}

----------------

using UnityEngine;
using System.Collections;

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

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;
    }
}
------------------


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;
    }
}