Keeping score

Now we have the basic gameplay in, but the player doesn't know how well they're doing to do that, we will.

  1. Go to GameObject | UI | Text. Double-click on the Canvas object in order to see it and how the text looks compared to it.
  2. Then, select the 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.
  3. Select the 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).
  4. We want this to be readable in most situations, so click on the Add Component menu and add an Outline component to this object as well with an Effect Color of white (Hex Color of FFFFFFFF) and an Effect Distance of 2, -2:
    Keeping score
  5. Now that we have the text created, let's add in the ability to gain a score. Go back to the GameController script add the following using:
    using UnityEngine.UI;
  6. Next, add the following variables to the file:
        private static Text scoreText;
        private static int score;
    
        public static int Score
        {
            get { return score; }
            set
            {
                score = value;
                scoreText.text = score.ToString();
            }
        }
  7. Afterwards, we need to initialize the variables, so add the following bolded lines to the Start function:
        // Use this for initialization
        void Start ()
        {
            speedModifier = 1.0f;
            gameObject.AddComponent<GameStartBehaviour>();
            score = 0;
            scoreText = 
            GameObject.Find("Score Text").GetComponent<Text>();
        }
  8. Save the script and now we need to increase the score when we go through the obstacles. To do this, we'll need to add in something to the obstacle prefab, so to make it easy to see the changes we'll be making, go to the Project tab and open up the 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:
    Keeping score
  9. From here, let's add a box collider. To do this, go to Component | Physics 2D | Box Collider 2D. From there, click on the Is Trigger option and change the Size to .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.

  10. Open up the ObstacleBehaviour script and add the following function:
        public void OnTriggerEnter2D(Collider2D collision)
        {
            GameController.Score++;
        }
  11. Save your script and then go back to the Unity editor. From there, select the Obstacle objects and then from the Inspector tab under the Prefab section, click on the Apply button.
  12. Delete the Hierarchy tab's Obstacle object and then save your project:
    Keeping score

Now we can see our score increasing properly and with that our project is completed!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset