Display the number of collected items

We need to draw an icon for the collected items and text to show the player the updated number of items collected. We put this information in the top-right corner, close to the spherical icon.

Getting ready

Open the GUI script in Monodevelop and be ready to follow our instructions.

How to do it…

  1. Add these lines to the OnGUI() function:
      void OnGUI(){
        for(int i=0; i<platScript.lives; i++){
          Graphics.DrawTexture(new Rect(30 + (60*i), 700, 50, 50), lifeIcon);
      }
    
        Graphics.DrawTexture(new Rect(880, 28, 30, 30), collIcon);
      }
  2. To display the text telling the player how many items they have collected, we need to do some extra work. First of all, create a new TextMesh in the game scene by navigating to GameObject | Create Other | 3D Text, as shown in this screenshot:
    How to do it…
  3. Name this text collText and drag it onto MainCamera in the scene so it becomes a child of MainCamera. We do this so that once we have set the desired position of the text with regard to the camera, the text moves along with it, keeping its relative position.
  4. Set the position of the text in the World Space. In the following screenshot, we provide the values we set for our prototype, considering the fact that we are displaying it with our viewport set as 1024 x 768 and Standalone:
    How to do it…
  5. Go back to the GUI script in Monodevelop. We need a few additions to it. There are actually two ways to display that text on the screen. One way is to add a public variable to the script to store a reference to the text in the scene and drag the 3D Text game object into the variable.

    The other way is to create two private variables: one to store a reference to the camera, and another to store the reference to the 3D Text attached to that camera. Then, in the Start() function, we instantiate the two variables to access the content we are searching for.

    The first approach is easier and more straightforward, so we pick that. Add the following variable declaration at the beginning of the GUI script:

      public TextMesh tm;
  6. Now go to the Update() function and add the following line to it:
      void Update () {
        tm.text = runScript.collected + " / 5";
      }
  7. The last step consists of dragging the 3D Text game object into the tm slot in the script attached to GUI from the Inspector panel, as shown in this screenshot:
    How to do it…
  8. If you play the game now, the icons representing the available lives are displayed in the bottom-left corner of the screen, while the icon and the number of items collected are displayed on the top-right corner. You may need to resize the game scene or the numeric parameters to display them correctly. The following screenshot shows the output we get on the screen:
    How to do it…

How it works…

Our approach to display the text on screen is pretty efficient; we use a single public variable to store the reference to 3D Text, and set the text we want to be displayed in the Update() function using the tm.text = runScript.collected + " / 5" line. We made the 3D text a child of MainCamera so it moves along the screen, keeping its relative position to the camera.

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

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