Now we have the basic gameplay in, but the player doesn't know how well they're doing to do that, we will.
Canvas
object in order to see it and how the text looks compared to it.Text
object and then from the Anchors Presets menu, hold down Alt+Shift and select the top-center option by clicking on it. Change the name of the object to Score Text
.Score Text
object and change the Rect Transform component's Height to 50
. Under the Text component, change the Text property to 0
and change the Alignment to center horizontally. Then, change the Font Size to 40
. I then changed the text color to a darker blue (If you want to use exactly what I did, you can click on the color and then for Hex Color, put in 7AA0ADFF
).FFFFFFFF
) and an Effect Distance of 2, -2
:using UnityEngine.UI;
private static Text scoreText; private static int score; public static int Score { get { return score; } set { score = value; scoreText.text = score.ToString(); } }
// Use this for initialization void Start () { speedModifier = 1.0f; gameObject.AddComponent<GameStartBehaviour>(); score = 0; scoreText = GameObject.Find("Score Text").GetComponent<Text>(); }
Prefabs
folder. From there, drag and drop an Obstacle
object into the Hierarchy tab and double-click on to zoom the camera so that we can see it easily:.5, 3
.The Is Trigger option allows us to tell when something collides with the collider, but instead of using physics to block the object from entering it, we can do something else using the OnTrigger
functions instead of OnCollision
. This is used oftentimes in games for things like spawning enemies when you enter a room. In this case, we will be increasing our score.
ObstacleBehaviour
script and add the following function:public void OnTriggerEnter2D(Collider2D collision) { GameController.Score++; }
Obstacle
objects and then from the Inspector tab under the Prefab section, click on the Apply button.Obstacle
object and then save your project:Now we can see our score increasing properly and with that our project is completed!