Monday, August 6, 2018

(Unity 4) Displaying text - 06 - Roll-a-ball - Unity Official Tutorials



Counting, displaying text and ending the game. We need a tool to store the value of our counted collectables. And another tool to add to that value as we collect and count them. Let's add this tool to our PlayerController script.

Select the player game object and open the PlayerController script for editing. Let's add a private variable to hold our count. This will be an int, as our count will be a whole number, we won't be collecting partial objects, and let's call it Count. So in our game we will first start with a count of 0.

Then we will need to increment our count value by 1 when we pick up a new object. First we need to set our count value to 0. As this variable is private we don't have any access to it in the Inspector. This variable is only available for use within this script and as such we will need to set it's starting value here in the script.

There are several ways we can set the starting value of Count, but in this assignment we will do it in the Start function. In Start set our Count to be equal to 0. Next we need to add to Count when we pick up our collectable game objects. We will pick up our objects in OnTriggerEnter if the Other game object has the tag Pickup.

So this is where we add our counting code. After setting the other game objects active state to False we add our new value of Count is equal to our old value of Count plus 1. There are other ways to add, count up or increment the value when coding for Unity, but this one is very easy to understand and this is the one that we're going to use in this assignment. Save this script and return to Unity.

Now we can store and increment our count but we have no way of displaying it. It would also be good to display a message when the game is over. To display text in this assignment we will be using simply GUI text objects to display our text and values. First let's create a new game object to hold the GUI text items.

Let's call the new game object DisplayText and make sure the transform is set to Origin. Next create a GUI text game object from the hierarchy's Create menu. Attach it as a child to the Display Text parent game object. Rename it Count Text.

We want our text to display in the upper left of the screen when the game is playing. We can see that the GUI text is currently displaying in the centre of the screen in the game view. This is because the transform of the GUI text game object is set to 0.5 And 90.5 In the X and Y. This is the centre of the viewport of the game view camera, which goes from 0.0 In the lower left up to 1.1 In the upper right.

Let's change the GUI text's transform to 0.1 Putting it in the top left of our camera's view port. Now it looks budged up against the corner of the game view. Let's give it some space between the text and the edges of the screen. We don't need to juggle the transform here, we can make small adjustments by setting the pixel offset property.

Let's bring it away from the side by 10 pixels and bring it down by 10 pixels That feels better now with some room around it. Yet it's still up and out of the way. So let's wire up this GUI text object to display our Count value. Start by opening the PlayerController script for editing.

Next create a new public GUI text variable called CountText to hold a reference to the GUI text component on our CountText game object. We need to set the starting value of the GUI. Text's text property. We can do this in Start as well Write countText.Text equals the string value of Count: plus count.ToString, and we need the parenthesis.

Now this line of code must be written after the line setting our count value. Count must have some value for us to set a text with. Now we also need to update this text property every time we pick up a new collectable So in OnTriggerEnter after we increment our count value let's write again countText.Text = 'Count: ' + count.ToString(); Hmm, we've now written the same line of code twice in the same script. This is generally bad form.

One way to make this a little more elegant is to create a function that does the work in one place and we simply call this function every time we need it. Let's create a new function with void SetCountText and then an empty set of parenthesis and brackets. Now let's move one instance of this line of code in to the function by cutting and pasting it. And in it's place let's put a line of code simply calling the function.

Finally let's replace the other line with a function call as well. Excellent. Save and swap back to Unity. Now we see our PlayerController script has a new GUI text property.

We can associate a reference to our Count text simply by dragging and dropping the CountText game object on to the slot. Unity will find the GUI text component on the game object and correctly associate the reference. Now let's save, enter play mode and test. Ah-ha! Not only do we collect these objects but we count them now.

Let's exit play mode. We need to display a message when we have collected all of the cubes. To do this we will need another GUI text object. Again, using the hierarchy Create menu make a new GUI text game object.

Rename it Win Text and place it in our Display Text parent game object. We want this text to display in the centre of the game screen but up a little so it doesn't cover the player game object. Simply adjust the transform position Y to 0.75 So we stay centred but up a little. Next let's adjust the anchor and alignment to middle centre and centre to centre the text.

Save the scene and swap back to our scripting editor. We need to add a reference for this new GUI text component. Create a new public GUI text variable and call it winText. Now let's set the starting value for the GUI text's text property.

This is set to an empty string or two double quote marks with no content. This text property will start empty. Then in the SetCountText function let's write if Count is greater than or equal to 12, which is the total number of objects we have in the game to collect, then our winText.Text equals You Win. Save this script and return to Unity.

Again on our player, our PlayerController has a new GUI text property. Associate the component again by dragging the Win Text game object on to the slot. Save the scene and play. So we're picking up our game objects, we're counting our collectables, and we win! And as we can see when we have collected 12 objects we display the 'You Win' text.

In the next and last assignment of this series we will build and deploy this game as a web player. Subtitles by the Amara.Org community.

(Unity 4) Displaying text - 06 - Roll-a-ball - Unity Official Tutorials

No comments:

Post a Comment