Getting to grips with Unity (C#)

After finishing my course at Bournemouth University, I wanted to invest my free time into improving my programming and game development skills.

I decided that since majority of my course at University had focused on the Unreal Development Kit, it would be cool to try out Unity properly and see how it compares.

I’ve done a small amount of work in Unity prior to this project, where I was following numerous online video tutorials and reading published books as I attempted to replicate some small and simple games.

BombArr

using UnityEngine;
using System.Collections;

public class GameScriptGrid : MonoBehaviour {

	public GameObject[] totalList;
	public Vector3[] initialStartPositions;
	public GameObject correctText;
	public GameObject incorrectText;
	private Color clickColour = new Color(5,0,0);
	public Color defaultColour;

	private AudioSource audioSource;
	public AudioClip correctClip;
	public AudioClip incorrectClip;

	private float correctClicks = 0;
	private float incorrectClicks = 0;
	private float level = 1;
	private float timeBetweenNewAdded = 0; //In miliseconds
	private float timeSinceLastChange = 0;

	public TextMesh textMesh1;
	public TextMesh textMesh2;
	public TextMesh textMesh3;


	
	// Use this for initialization
	void Start () 
	{
		defaultColour = totalList [0].renderer.material.color;
		audioSource = GetComponent<AudioSource> ();
		initialStartPositions = new Vector3[totalList.Length];
		for (int i = 0; i < totalList.Length; i++)
						initialStartPositions [i] = totalList [i].transform.position;
	}
	
	// Update is called once per frame
	void Update () 
	{
		ChangeMaxSimultaneousBasedOnLevel ();
		timeBetweenNewAdded = 2000 / level;

		if (timeSinceLastChange < (Time.realtimeSinceStartup * 1000)) 
		{
			Debug.Log("Add New Cube Time!");
			int cubeNo = GetRandomCube();

			if (cubeNo > -1)
			{
				totalList[cubeNo].GetComponent<CubeScript>().ColorToChangeTo = clickColour;
				totalList[cubeNo].GetComponent<CubeScript>().SetActive();
			}
			timeSinceLastChange = (Time.realtimeSinceStartup * 1000) + timeBetweenNewAdded;
			
		}
		DoInput ();

		if (level > 4)
			MoveThemRightLeft ();
		if (level > 6)
			Camera.main.GetComponent<MainCameraScript> ().spin = true;
		SimpleSway ();
	}

	public bool goRight = true;

	void SimpleSway()
	{
		float x = this.transform.position.x;

		if (goRight)x = x + (0.1f * Time.deltaTime);
		else x = x - (0.1f * Time.deltaTime);
		if (x > 0.2f)
						goRight = false;
		if (x < -0.2f)
						goRight = true;
		this.transform.position = new Vector3 (x, this.transform.position.y, this.transform.position.z);
						
	}

	int GetRandomCube()
	{
		int max = totalList.Length;
		int val = Random.Range (0, max);
		if (totalList [val].renderer.material.color == clickColour)
						return -1;
				else
						return val;
	}

	private float timeCounter = 0f;
	private float shiftTime = 2.0f;
	private float moveLeftRightSpeed = 0.5f;

	void MoveThemRightLeft()
	{
		timeCounter += Time.deltaTime;

		if (timeCounter > shiftTime) 
		{
			Debug.Log("CHANGE NOW...");
			timeCounter = 0f;
			shiftTime = 4f;
			moveLeftRightSpeed = -moveLeftRightSpeed;
		}

		for (int i = 0; i < totalList.Length; i++) {
			if (i < 3) totalList[i].transform.position = new Vector3(totalList[i].transform.position.x + moveLeftRightSpeed * Time.deltaTime,
			                                                         totalList[i].transform.position.y,
			                                                         totalList[i].transform.position.z);
			else if (i < 6) totalList[i].transform.position = new Vector3(totalList[i].transform.position.x - moveLeftRightSpeed * Time.deltaTime,
			                                                                 totalList[i].transform.position.y,
			                                                                 totalList[i].transform.position.z);
			else totalList[i].transform.position = new Vector3(totalList[i].transform.position.x + moveLeftRightSpeed * Time.deltaTime,
			                                                         totalList[i].transform.position.y,
			                                                         totalList[i].transform.position.z);
		}
	}
	
	void ChangeMaxSimultaneousBasedOnLevel()
	{
		float tmpLvl = (int)(correctClicks / 3f);
		//Debug.Log ("(int)(correctClicks / 2f): " + tmpLvl);

		if (tmpLvl > 100) {
			tmpLvl = 11 + (tmpLvl / 50);
		}
		else
			tmpLvl = 1 + (tmpLvl / 10);
		//Debug.Log ("LEVEL: " + tmpLvl);
		level = tmpLvl;
		textMesh1.text = "Level: " + ((level * 10) - 9).ToString();
	}

	private void DoInput()
	{
		if(Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit[] rayCastHit;
			rayCastHit = Physics.RaycastAll(ray);
			if (rayCastHit.Length > 0)
			{
				CubeScript temp = rayCastHit[0].transform.gameObject.GetComponent<CubeScript>();
				Debug.Log("SOMETHING HIT...");
				Debug.Log(rayCastHit[0].transform.gameObject.name);

				if (temp.isActivated)
				{
					correctClicks++;
					temp.SetDefault();
					audioSource.clip = correctClip;
					audioSource.Play();
					Instantiate(correctText, temp.transform.position,Quaternion.identity);
				}
				else
				{
					//Incorrect Hit
					Instantiate(incorrectText, temp.transform.position, Quaternion.identity);
					audioSource.clip = incorrectClip;
					audioSource.Play();
					incorrectClicks++;
					textMesh2.text = "Misses: " + incorrectClicks.ToString() + " / 5";
				}
			}
		}

	}
}
using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour {

	public bool isActivated = false;
	public Color ColorToChangeTo;
	public float LerpSpeed = 0.025f;

	private float speedLerp;
	private Color defaultCol;
	private float timeActivated = 0.0f;
	private Vector3 startingScale;
	public float timeToDestroy = 1500;
	private float startingYValue;

	public GameObject explosion;

	public ParticleSystem ps;

	private float yPositionDesired = 4;

	// Use this for initialization
	void Start () 
	{
		startingScale = this.transform.localScale;
		speedLerp = LerpSpeed;
		ColorToChangeTo = defaultCol = this.renderer.material.color;
		startingYValue = this.transform.position.y;
	}
	
	// Update is called once per frame
	void Update () 
	{
		//if (this.renderer.material.color != ColorToChangeTo)
			this.renderer.material.color = Color.Lerp (this.renderer.material.color, ColorToChangeTo, speedLerp);

		if (isActivated) {
			if ((timeActivated + timeToDestroy) < ((Time.realtimeSinceStartup * 1000))) {
				transform.localScale += new Vector3 (0.0025f, 0.0025f, 0.0025f);
				//speedLerp = 0.075f;
				//ColorToChangeTo = new Color (0, 0, 0);

				if ((timeActivated + timeToDestroy * 1.5f) < ((Time.realtimeSinceStartup * 1000))) {
					Debug.Log ("DESTORY???");
					//GAME OVERRRRR
					isActivated = false;
					Instantiate(explosion, this.transform.position, Quaternion.identity);
					ps.Stop ();
					GetComponent<MeshRenderer> ().enabled = false;
				}
			}
			

		} else {
			transform.localScale = startingScale;
		}

		Debug.Log ("Y POS: " + this.transform.position.y);
		this.transform.position = Vector3.Lerp( this.transform.position,
		                                       new Vector3 (this.transform.position.x,
		            yPositionDesired,
		                                                    this.transform.position.z), 
		                                       speedLerp);
	}
	
	public void SetActive()
	{

		ps.Play ();
		speedLerp = LerpSpeed;
		timeActivated = Time.realtimeSinceStartup * 1000;
		isActivated = true;
	}

	public void SetDefault()
	{
		this.transform.position = new Vector3 (this.transform.position.x,
		                                       3.5f,
		                                       this.transform.position.z);
		ps.Stop ();
		speedLerp = LerpSpeed;

		ColorToChangeTo = defaultCol;
		isActivated = false;
	}
}