Displaying multiple pickups of the same object with text totals

When several items of the same type have been picked up, often the simplest way to convey what is being carried to the user is to display a text message showing the numeric total of each item type being carried, as shown in the following screenshot. In this recipe, the total number of stars collected is displayed using a UI Text object.

Displaying multiple pickups of the same object with text totals

Getting ready

This recipe assumes you are starting with project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter. The font you need can be found in folder 1362_02_02.

How to do it...

To display inventory total text for multiple pickups of same type of object, follow these steps:

  1. Start with a new copy of the mini-game Simple2Dgame_SpaceGirl.
  2. Add a UI Text object (Create | UI | Text). Rename it Text-carrying-star. Change its text to stars = 0.
  3. Import the provided Fonts folder into your project.
  4. In the Inspector panel, set the font of Text-carrying-star to Xolonium-Bold (folder Fonts) and set its color to yellow. Center the text horizontally and vertically, and set its Font Size to 32.
  5. In its Rect Transform component, set its Height to 50. Edit its Rect Transform, and while holding down SHIFT and ALT (to set pivot and position), choose the top-stretch box. Your text should now be positioned at the middle top of the Game panel, and its width should stretch to match that of the whole panel.
  6. Add the following C# Script Player to GameObject player-SpaceGirl in the Hierarchy:
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
      public Text starText;
      private int totalStars = 0;
    
      void Start(){
        UpdateStarText();
      }
    
      void OnTriggerEnter2D(Collider2D hit){
        if(hit.CompareTag("Star")){
          totalStars++;
          UpdateStarText();
          Destroy(hit.gameObject);
        }
      }
    
      private void UpdateStarText(){
        string starMessage = "stars = " + totalStars;
        starText.text = starMessage;
      }
    }
  7. From the Hierarchy view, select the GameObject player-SpaceGirl. Then, from the Inspector, access the Player (Script) component and populate the Star Text public field with UI Text object Text-carrying-star.
  8. Select the GameObject star in the Hierarchy panel and make three more copies of this GameObject.

    Note

    Note: Use keyboard shortcut CTRL + D (Windows) or CMD + D (Mac) to quickly duplicate GameObjects.

  9. Move these new GameObject to different parts of the screen.
  10. Play the game—each time you pick up a star, the total should be displayed in the form stars = 2.

How it works...

The Text variable starText is a reference to the UI Text object Text-carrying-star. The int variable totalStars represents how many stars have been collected so far; it is initialized to zero.

In the OnTriggerEnter2D() method, the totalStars counter is incremented by 1 each time the player's character hits an object tagged Star. The collided star GameObject is destroyed and a call is made to the UpdateStarText()method.

The UpdateStarText() method updates the text content of UI Text object Text-carrying-star with text string stars = concatenated with the integer value inside variable totalStars to display the updated total number of stars to the user.

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

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