Tutorial 2: Unity 2D Catch Game (beginners). Part 2

Introduction:

Here is the second part of Catch Game 2D tutorial. You can check the first part here . In the tutorial we will add basic UI, some sounds and other little tweak for 2D catch game prototyp.
Later we will need three sound files for this tutorial. You can download Zip archive with 3 files here. Or just use your own sounds.

1. Step

Ok, let's go. Start Unity. Open your project with Catch Game 2D tutorial (maybe you have other name for it). Load scene catchGame. You can just double click on it in Assets window.
If you don't have this tutorial, then you can download unity package here. Start Unity, click on New Project, set it to 2D, name it and click Create. If Unity editor is opened, then click Assets -> Import Package -> Custom Package. Select catchGame package in your download folder and open it. Then click on Import.
Import of the package can be bugged. Gameobjects tags can be reseted to "undefinied". So you should add "Bad" and "Good" tags to falling gameobjects. Check 5. and 6. Step of 1. tutorial.
Your editor should be now like this:
catch game screenshot

2. Step

Our Player can fall from the scene on left or right side. We can place walls here to avoid this. Add new Empty Gameobject in scene (click on GameObject -> Create Empty). Rename it as LeftWall (right click on it in Hierarchy window). Add to LeftWall Box Collider 2D (select LeftWall in Hierarchy, go to it Inspector, click on Add Component -> Physics 2D -> Box Collider 2D). Click on Edit Collider in Inspector. Adjust the size of the collider in scene as wall (click on green points and pull them). Select Moving Tool and move LeftWall on left scenes side. Set its Z Transform Position to zero. You can add icon to it for better view in Inspector.
left wall
We need one RightWall. We can just duplicate the LeftWall. Choose LeftWall in Hierarchy. Right-click on it and click on Duplicate. Select the duplicated object. Rename it to RightWall. Enable Moving Tool (if not enabled) and move the RightWall on right side. Now we we have walls and our Player can not fall.
right wall

3. Step

We can make simple interface to show our score on screen. Click on GameObject-> UI-> Text. Now Canvas, Text and EventSystem will be created.
UI text
Important: we have now scene-view and UI-view in one window. Double click on Canvas in Hierarchy. We will see white box. This box shows how the UI will look on screen. Set UI Scale Mode to Scale with Screen Size.
UI canvas
Select Text in Hierarchy. Rename it to Score. Double click on it to focus it for better view. Go to scores Inspector. Type 00 in Text field. Set Alignment to middle. Enable Best Fit. Click on Color and pick one color that you like (I have yellow). Double click on Canvas in Hierarchy for good UI-view (you can zoom little). Select moving tool, select Score and move somewhere on top in UI-Box. You can enable rectangle tool and adjust Scores size.
You can use rectangle tool for moving too. Use moving tool for small objects.
Start the game and check if Score is looking good (or you can switch to Game tab and then back to Scene tab).
text field

4. Step

We need now one script to control the number output from Score gameobject. We can do little change in itemChecker script. Open itemChecker script (double click on it in Assets window). If we will work with UI - Unity Interface in our script, we should use UI namespace.
using UnityEngine.UI;
At first we need one reference to our Score.
public GameObject ScoreObject;
We can put later our Score GameObject in this field in Inspector. The Score gameobject has Componente Text. Now we should make reference to this componente on Score gameobject. We should declare Text field. And then we can use GetComponente for reference to Text componente.
Text scoreText;
scoreText = ScoreObject.GetComponent < Text >();
We have now reference to Text componente. This componente has string variable text, which is displayed on screen. We can change the text variable. In our script we have int variable score, which can be transformed to string variable. This can be easy done with variable.toString() funktion.
scoreText.text = score.ToString();
Our itemChecker script should be now:
using UnityEngine;
using System.Collections;
//we need this for work with UI
using UnityEngine.UI;

public class itemChecker : MonoBehaviour {

//variable for score counting
public int score;
//variable for reference to Score GameObject
public GameObject ScoreObject;
//variable for reference to Text componente on Score GameObject
Text scoreText;

//will be executed once
void Start() {
//reference scoreText to Text component
scoreText = ScoreObject.GetComponent < Text >();
}

//will be executed if one Collider2D went into Trigger2D
void OnTriggerEnter2D (Collider2D other) {
//check if collided gameobject has tag Good
if (other.gameObject.tag == "Good") {
//increase score
score = score + 10;
//set string text to score value, which was converted from int to string
scoreText.text = score.ToString();
//delete collided gameobject from the scene
Destroy (other.gameObject);
}
//check if collided gameobject has tag Bad
if (other.gameObject.tag == "Bad") {
//decrease score value
score = score - 10;
//set string text to score value, which was converted from int to string
scoreText.text = score.ToString();
//delete collided gameobject from the scene
Destroy (other.gameObject);
}
}
}

5. Step

Select Player gameobject. Look at its Inspector. ItemChecker script has now new field Score Object. Drag&Drop Score gameobject from Hierarchy in this field (or click on select point right from ScoreObject field and select Score). Start game and check if score changes.
score reference

6. Step

We can import some audio files now. Entpack downloaded Sound.zip (you can download it here.). Import audio files (Boom, Ok, Music) in Unity (you can drag&drop them from entpacked folder in Assets window in Unity. Or right click in Assets window, select Import New Asset and choose the audio files and click import). You can use your own audio files too.
import asset

7. Step

Simply background music can be easy made in Unity. Select Main Camera in Hierarchy. Drag&Drop Music from Assets on Inspector (empty field)(or you can click Add Component -> Audio -> Audio Source. Click on select point (right from AudioClip field and select Music)). Enable Loop. You can adgjust Volume too. Start game and check if music is working.
background music

8.Step

We can play sound if one gameobject was catched. We should make little change on itemChecker script for that. At first we should declare two audioclips in Script for Boom and Ok sounds.
public AudioClip OkSound, BoomSound;
For sound playing we can use AudioSource.PlayClipAtPoint(sound, position); funktion. The funktion makes new gameobject, that will play the sound at the defined position and then destroys themself. For OkSound it will be:
AudioSource.PlayClipAtPoint(OkSound, other.transform.position);
other.transform.position is position from the gameobject, that went in Trigger2D.
The itemChecker script will be now:
using UnityEngine;
using System.Collections;
//we need this for work with UI
using UnityEngine.UI;

public class itemChecker : MonoBehaviour {

//variables for sound files
public AudioClip OkSound, BoomSound;
//variable for score counting
public int score;
//variable for reference to Score GameObject
public GameObject ScoreObject;
//variable for reference to Text componente on Score GameObject
Text scoreText;

//will be executed once
void Start() {
//reference scoreText to Text component
scoreText = ScoreObject.GetComponent < Text >();
}

//will be executed if one Collider2D went into Trigger2D
void OnTriggerEnter2D (Collider2D other) {
//check if collided gameobject has tag Good
if (other.gameObject.tag == "Good") {
//increase score
score = score + 10;
//set string text to score value, which was converted from int to string
scoreText.text = score.ToString();
//play audio file at position from catched gameobject
AudioSource.PlayClipAtPoint(OkSound, other.transform.position);
//delete collided gameobject from the scene
Destroy (other.gameObject);
}
//check if collided gameobject has tag Bad
if (other.gameObject.tag == "Bad") {
//decrease score value
score = score - 10;
//set string text to score value, which was converted from int to string
scoreText.text = score.ToString();
//play audio file at position from catched gameobject
AudioSource.PlayClipAtPoint(BoomSound, other.transform.position);
//delete collided gameobject from the scene
Destroy (other.gameObject);
}
}
}

9.Step

Select Player now in Hierarchy. itemChecker script has now two new fields for audio files. Drag&Drop Ok from Assets on Ok Sound field (or use select point rigt from this field and select Ok). Drag&Drop Boom from Assets on Boom Sound field.
add sound files
Start game and check it.

10. Step

Ok, it will not be here step 10. There are many things that could be changed here in the game. As example, we could add trigger2D on player's side. The side triggers could change the tags from falling objects to untaged. Then the player will not catch the objects from the side. I wanted just show basics from Unity engine. So start easy. Don't make something difficult if you are beginner. Learn one simple thing at first. Later with known basics you can make more. As example, this is not only catch game tutorial. Replace gameobjects with cars, background with moving road and you will have one simple car game ;)

download UnityPackage download unity package of this tutorial.
Import of the package can be bugged. Gameobjects tags can be reseted to undefinied. So you should add Bad and Good tags to falling gameobjects. Check 5. and 6. Step of 1. tutorial.