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.
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); }
TextMesh
in the game scene by navigating to GameObject | Create Other | 3D Text, as shown in this screenshot: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;
Update()
function and add the following line to it:void Update () { tm.text = runScript.collected + " / 5"; }
tm
slot in the script attached to GUI from the Inspector panel, as shown in this screenshot: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.
3.12.163.175