LinkedIn Recommendation

CC Image Puppy by Jonathan Kriz at Flickr

It isn’t often that you meet someone as talented as Terra. Terra and I have worked on two game design projects together this year. She is a very detail-oriented person and has incredible artistic abilities. Not only is she talented, but she is also very easy and fun to work with. All in all, anyone would be happy to have Terra on their team.

GetComponent

Evidence

Script #1

using UnityEngine;
using System.Collections;

public class UsingOtherComponents : MonoBehaviour
{
    public GameObject otherGameObject;

    private AnotherScript anotherScript;
    private YetAnotherScript yetAnotherScript;
    private BoxCollider boxCol;

    void Awake()
    {
        anotherScript = GetComponent<AnotherScript>();
        yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>();
        boxCol = otherGameObject.GetComponent<BoxCollider>();
    }

    void Start()
    {
        boxCol.size = new Vector3(3, 3, 3);
        Debug.Log(“The player’s score is “ + anotherScript.playerScore);
        Debug.Log(“The player has died “ + yetAnotherScript.numberOfPlayerDeaths + ” times”);
    }
}

Script #2

using UnityEngine;
using System.Collections;

public class AnotherScript : MonoBehaviour
{
    public int playerScore = 9001;
}

Script #3

using UnityEngine;
using System.Collections;

public class YetAnotherScript : MonoBehaviour
{
    public int numberOfPlayerDeaths = 3;
}

Screencast

What I Learned

This tutorial taught me how to reference different scripts and GameObjects through one script so that their values can be changed in the hierarchy view of Unity. The scripts were relatively simple to copy down and the tutorial was easy to follow, so the only problem I had was making sure everything was accurate in the scripts so they would function correctly when attached to my cube.

GetAxis

Evidence

Script #1

using UnityEngine;
using System.Collections;

public class AxisExample : MonoBehaviour
{
    public float range;
    public GUIText textOutput;

    void Update()
    {
        float h = Input.GetAxis(“Horizontal”);
        float xPos = h * range;

        transform.position = new Vector3(xPos, 2f, 0);
        textOutput.text = “Value Returned: “ + h.ToString(“F2”);
    }
}

Script #2

using UnityEngine;
using System.Collections;

public class AxisRawExample : MonoBehaviour
{
    public float range;
    public GUIText textOutput;

    void Update()
    {
        float h = Input.GetAxisRaw(“Horizontal”);
        float xPos = h * range;

        transform.position = new Vector3(xPos, 2f, 0);
        textOutput.text = “Value Returned: “ + h.ToString(“F2”);
    }
}

Script #3

using UnityEngine;
using System.Collections;

public class DualAxisExample : MonoBehaviour
{
    public float range;
    public GUIText textOutput;

    void Update()
    {
        float h = Input.GetAxis(“Horizontal”);
        float v = Input.GetAxis(“Vertical”);
        float xPos = h * range;
        float yPos = v * range;

        transform.position = new Vector3(xPos, yPos, 0);
        textOutput.text = “Horizontal Value Returned: “ + h.ToString(“F2”) + “\nVertical Value Returned: “ + v.ToString(“F2”);
    }
}

Screencast

What I Learned

This tutorial taught me how to attach a script to an object that makes it move on an axis to the left or right and possibly up and down if you use the dual axis script. It works similarly to the GetButton script. One problem I had to solve was in the inspector view there is an option to change the range the object can move. At first I hadn’t changed the range from 0 so I was extremely confused as to why my object wouldn’t move. After figuring that out the rest was smooth sailing.

Update and FixedUpdate

Evidence

Script

using System.Collections;
using UnityEngine;

public class UpdateAndFixedUpdate : MonoBehaviour
{
    void FixedUpdate()
    {
        Debug.Log(“FixedUpdate time :” + Time.deltaTime);
    }

    void Update()
    {
        Debug.Log(“Update time :” + Time.deltaTime);
    }
}

Screencast

What I Learned

This tutorial taught me the difference between using Update and FixedUpdate in a script. Attaching the script to and object and using the Debug function helped show the difference in the console view. FixedUpdate updated physics every 0.2 seconds while the other was more random. One problem I had to solve was making sure my script was attached to the right object and was accurate.

Awake and Start

Evidence

Script

using System.Collections;
using UnityEngine;

public class AwakeAndStart : MonoBehaviour
{
    void Awake()
    {
        Debug.Log(“Awake called.”);
    }

    void Start()
    {
        Debug.Log(“Start called.”);
    }
}

Screencast

What I Learned

This tutorial shows how to use the awake and start functions in a script. It clarifies the order in which they appear, and how they should be used. In my screencast I show how the awake and start functions appear in the console view. One problem I had to solve was actually finding the console view in Unity so I could record it. Because of this I had a couple rough first takes of the screencast, but otherwise the tutorial was easy to follow and didn’t take too long to do.

 

Enabling and Disabling Components

Evidence

Script

using System.Collections;
using UnityEngine;

public class EnableComponents : MonoBehaviour 
{
    private Light myLight;

    void Start () 
    {
        myLight = GetComponent<Light>();
    }
    

    void Update ()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            myLight.enabled = !myLight.enabled;
        }
    }
}

Screencast

What I Learned

In doing this tutorial I learned how to enable and disable components through a script which includes a toggle function through the space bar. Specifically, this tutorial showed me how to turn a light on and off through the script. One problem I had to solve was making sure my code was accurate and without errors. Otherwise this tutorial was pretty problem free.

Translate and Rotate

Evidence

Script

using System.Collections;
using UnityEngine;

public class TransformFunctions : MonoBehaviour 
{
    public float moveSpeed = 10f;
    public float turnSpeed = 50f;

    void Update () 
    {
        if (Input.GetKey(KeyCode.UpArrow))
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        if (Input.GetKey(KeyCode.DownArrow))
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        if (Input.GetKey(KeyCode.LeftArrow))
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);

        if (Input.GetKey(KeyCode.RightArrow))
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    }
}

Screencast

What I Learned

For this project I learned how to make an object move through a script. The object moves by pressing the arrow keys, and the turn speed and move speed can both be altered in either the script or the hierarchy view. A problem I had to solve was making sure I got every detail of the code right, as there are a lot of pieces for this specific tutorial.

Look At

Evidence

Script

using System.Collections;
using UnityEngine;

public class CameraLookAt : MonoBehaviour 
{
    public Transform target;

    void Update () 
    {
        transform.LookAt(target);
    }
}

Screencast

What I Learned

For this project I learned how to attach a script to a camera so that it looks at a certain object. This was definitely one of the more easier tutorials, as I didn’t have any big bumps like the first couple. One problem I had to solve was that I didn’t have the robot lab assets handy so I just used a simple sphere and plane. I also had to watch the video a couple times over to make sure I got all the details.

Scope and Access Modifiers

Script #1

using System.Collections;
using UnityEngine;

public class ScopeAndAccessModifiers : MonoBehaviour 
{
    public int alpha = 5;

    private int beta = 0;
    private int gamma = 5;

    private AnotherClass myOtherClass;

    void Start () 
    {
        alpha = 29;

        myOtherClass = new AnotherClass();
        myOtherClass.FruitMachine(alpha, myOtherClass.apples);
    }

    void Example (int pens, int crayons) 
    {
        int answer;
        answer = pens * crayons * alpha;
        Debug.Log(answer);
    }

    void Update () 
    {
        Debug.Log(“Alpha is set to: “ + alpha);
    }
}

Script #2

using System.Collections;
using UnityEngine;

public class AnotherClass : MonoBehaviour
{
    public int apples;
    public int bananas;

    private int stapler;
    private int sellotape;

    public void FruitMachine(int a, int b)
    {
        int answer;
        answer = a + b;
        Debug.Log(“Fruit total: “ + answer);
    }

    private void OfficeSort(int a, int b)
    {
        int answer;
        answer = a + b;
        Debug.Log(“Office Supplies total: + answer”);
    }
}

Screencast

What I Learned

In this tutorial the focus was to show you how to change scope and access modifiers within script. With the script attached to the object, you can then put intervals that show up in the inspector view so you can change the intervals with Unity instead of going back and changing the script. One problem I had to solve was that I didn’t realize I had an extra “s” in the title of my script, so an error popped up in Unity that my script couldn’t be used. This actually took me half the period to figure out because it was so tiny and I’m a little tired as I go through making these.

Activating Game Objects

Evidence

Script #1

using System.Collections;
using UnityEngine;

public class script : MonoBehaviour 
{

    void Start () 
    {
        gameObject.SetActive(false);
    }
}

Script #2

using System.Collections;
using UnityEngine;

public class scriptscript : MonoBehaviour 
{
    public GameObject myObject;

    // Use this for initialization
    void Start() 
    {
        Debug.Log(“Active Self: “ + myObject.activeSelf);
        Debug.Log(“Active in Hierarchy: “ + myObject.activeInHierarchy);
    } 
}

Screencast

What I Learned

After watching this tutorial I learned how to activate game objects in a scene using SetActive and activeSelf / activeInHierarchy tools. This helped me learn some new code that could be useful in the future. One problem I had to solve was at first I didn’t realize I had two scripts that were supposed to be for two separate objects located as a component for only one object. It took me a couple minutes of searching to locate the problem and fix which script was attached to which object, but after I figured that out the objects acted just as they were supposed to.