‫Welcome back.
‫In this video, we are going to change the value of that text element programmatically.
‫So let's go ahead and create a empty object and let's call that one game manager.
‫A game manager is an object that you can use, for example, to use to collect your score, to take
‫care of states and those kinds of things.
‫And if you want to add this functionality that we can change the number here, let's go ahead and create
‫a new script.
‫And I'm going to call that text behavior.
‫So let's open it up.
‫Let's open that script tab.
‫And whenever you want to work with text within code, what you need to do or with text elements, what
‫you need to do is you need to add using unity engine dot ui.
‫So this namespace is important.
‫Otherwise you can't create a text object.
‫So you can see there is a class called text mask graphic.
‫I lay out element, so it's an default graphic to draw font data to screen and that's what we want.
‫So I'm going to create a public text and I'm going to call it simply my text.
‫Now, within the START method, I could go ahead and initialize that so I could search for a text,
‫or I can simply do it like that so that I can drag it in to my.
‫Component.
‫You will see what that means in a second.
‫Now, if somebody presses on a button, if input not get key, down is key code dot space.
‫So whenever we press paste, what I want to happen is I want to change my text and I want to change
‫it to a number.
‫So let's create a number int text number and let's set that to zero.
‫And every time that I hit space, I want text number to be increased by one.
‫So I increase increment the number of text number by one every time that somebody presses on the space
‫key.
‫And at the same time I want to change the text of my text.
‫So my text dot text is equal to text number dot text number dot to string.
‫So what that will do is it will change the the text itself to the value that our text number has.
‫And of course, we need to cast it into a string because text expects a string.
‫As you can see, string text.
‫So the string value this text will the text will display.
‫So my text, which is our text UI element, will now display whatever number we have there.
‫So let's have a look what that means.
‫If we go back to unity, our text behavior that our game manager has now we'll get my text, which is
‫our text from the canvas that I have here.
‫So I have to drag it in there.
‫Now we have a text in here and what we can do is run the code.
‫And see what happens if I press space.
‫So I'm pressing space.
‫And as you can see, the number goes up every time I press space and it's displayed here.
‫You could even go ahead and say.
‫If, however, the input that was pressed is not so else.
‫If
‫so, if the input is not the space, but let's say the SW key was pressed.
‫So get key down key code dot SW.
‫If that's the case, then change my text
‫thought text to SW was pressed.
‫Now let's see what happens.
‫Let's go back to our game.
‫Let's start that.
‫Now you can see it's a zero.
‫I press base and I press SW and you can see as was pressed.
‫Now of course it's not perfectly positioned.
‫So our text UI element was not perfectly positioned to maintain this amount of information.
‫So this amount of characters we could, however of course change that.
‫So we could change the width of it or we could move it over and we could even change the width to,
‫let's say, 300 and with a little bit further even so now let's see what happens now.
‫But wait, let's save our scene.
‫That's always good.
‫So let's call that one main control s and now let's run it.
‫Now we press enter, enter, enter and press se and you see se was pressed.
‫All right, so now you know how you can manipulate your text elements, how you can use them in general.
‫Because what we will do is, of course we will use them in the Pong game and in general in every game
‫because we will need a UI.
‫And that's for example how you can use text.