1 00:00:04,260 --> 00:00:05,040 ‫Welcome back. 2 00:00:05,070 --> 00:00:09,820 ‫In this video, we are going to change the value of that text element programmatically. 3 00:00:09,840 --> 00:00:17,490 ‫So let's go ahead and create a empty object and let's call that one game manager. 4 00:00:18,540 --> 00:00:25,680 ‫A game manager is an object that you can use, for example, to use to collect your score, to take 5 00:00:25,680 --> 00:00:28,290 ‫care of states and those kinds of things. 6 00:00:28,290 --> 00:00:34,590 ‫And if you want to add this functionality that we can change the number here, let's go ahead and create 7 00:00:34,590 --> 00:00:36,330 ‫a new script. 8 00:00:36,810 --> 00:00:41,970 ‫And I'm going to call that text behavior. 9 00:00:45,210 --> 00:00:46,470 ‫So let's open it up. 10 00:00:46,560 --> 00:00:47,910 ‫Let's open that script tab. 11 00:00:50,300 --> 00:00:58,130 ‫And whenever you want to work with text within code, what you need to do or with text elements, what 12 00:00:58,130 --> 00:01:03,580 ‫you need to do is you need to add using unity engine dot ui. 13 00:01:03,590 --> 00:01:05,660 ‫So this namespace is important. 14 00:01:05,660 --> 00:01:10,880 ‫Otherwise you can't create a text object. 15 00:01:10,880 --> 00:01:14,690 ‫So you can see there is a class called text mask graphic. 16 00:01:14,690 --> 00:01:21,170 ‫I lay out element, so it's an default graphic to draw font data to screen and that's what we want. 17 00:01:21,170 --> 00:01:29,840 ‫So I'm going to create a public text and I'm going to call it simply my text. 18 00:01:31,280 --> 00:01:36,530 ‫Now, within the START method, I could go ahead and initialize that so I could search for a text, 19 00:01:36,530 --> 00:01:41,540 ‫or I can simply do it like that so that I can drag it in to my. 20 00:01:42,530 --> 00:01:43,210 ‫Component. 21 00:01:43,220 --> 00:01:45,080 ‫You will see what that means in a second. 22 00:01:45,740 --> 00:01:58,010 ‫Now, if somebody presses on a button, if input not get key, down is key code dot space. 23 00:01:58,220 --> 00:02:05,300 ‫So whenever we press paste, what I want to happen is I want to change my text and I want to change 24 00:02:05,300 --> 00:02:06,050 ‫it to a number. 25 00:02:06,050 --> 00:02:14,870 ‫So let's create a number int text number and let's set that to zero. 26 00:02:15,500 --> 00:02:19,880 ‫And every time that I hit space, I want text number to be increased by one. 27 00:02:20,270 --> 00:02:28,160 ‫So I increase increment the number of text number by one every time that somebody presses on the space 28 00:02:28,190 --> 00:02:28,700 ‫key. 29 00:02:29,240 --> 00:02:33,440 ‫And at the same time I want to change the text of my text. 30 00:02:33,440 --> 00:02:44,540 ‫So my text dot text is equal to text number dot text number dot to string. 31 00:02:46,460 --> 00:02:53,480 ‫So what that will do is it will change the the text itself to the value that our text number has. 32 00:02:53,480 --> 00:02:59,360 ‫And of course, we need to cast it into a string because text expects a string. 33 00:03:00,920 --> 00:03:02,870 ‫As you can see, string text. 34 00:03:03,020 --> 00:03:06,440 ‫So the string value this text will the text will display. 35 00:03:06,440 --> 00:03:15,350 ‫So my text, which is our text UI element, will now display whatever number we have there. 36 00:03:15,350 --> 00:03:17,270 ‫So let's have a look what that means. 37 00:03:17,600 --> 00:03:24,650 ‫If we go back to unity, our text behavior that our game manager has now we'll get my text, which is 38 00:03:24,650 --> 00:03:26,840 ‫our text from the canvas that I have here. 39 00:03:26,840 --> 00:03:29,390 ‫So I have to drag it in there. 40 00:03:29,420 --> 00:03:34,280 ‫Now we have a text in here and what we can do is run the code. 41 00:03:35,530 --> 00:03:37,810 ‫And see what happens if I press space. 42 00:03:37,810 --> 00:03:39,130 ‫So I'm pressing space. 43 00:03:39,130 --> 00:03:45,760 ‫And as you can see, the number goes up every time I press space and it's displayed here. 44 00:03:47,380 --> 00:03:49,300 ‫You could even go ahead and say. 45 00:03:50,710 --> 00:03:58,630 ‫If, however, the input that was pressed is not so else. 46 00:03:58,630 --> 00:03:59,350 ‫If 47 00:04:02,410 --> 00:04:08,300 ‫so, if the input is not the space, but let's say the SW key was pressed. 48 00:04:08,320 --> 00:04:12,580 ‫So get key down key code dot SW. 49 00:04:13,810 --> 00:04:17,440 ‫If that's the case, then change my text 50 00:04:20,470 --> 00:04:26,830 ‫thought text to SW was pressed. 51 00:04:29,350 --> 00:04:30,700 ‫Now let's see what happens. 52 00:04:30,970 --> 00:04:32,620 ‫Let's go back to our game. 53 00:04:32,620 --> 00:04:33,670 ‫Let's start that. 54 00:04:36,480 --> 00:04:37,800 ‫Now you can see it's a zero. 55 00:04:37,830 --> 00:04:42,720 ‫I press base and I press SW and you can see as was pressed. 56 00:04:43,020 --> 00:04:46,230 ‫Now of course it's not perfectly positioned. 57 00:04:46,230 --> 00:04:54,150 ‫So our text UI element was not perfectly positioned to maintain this amount of information. 58 00:04:54,150 --> 00:04:59,610 ‫So this amount of characters we could, however of course change that. 59 00:04:59,610 --> 00:05:06,960 ‫So we could change the width of it or we could move it over and we could even change the width to, 60 00:05:06,960 --> 00:05:13,590 ‫let's say, 300 and with a little bit further even so now let's see what happens now. 61 00:05:14,520 --> 00:05:16,260 ‫But wait, let's save our scene. 62 00:05:16,260 --> 00:05:17,160 ‫That's always good. 63 00:05:17,310 --> 00:05:21,810 ‫So let's call that one main control s and now let's run it. 64 00:05:23,730 --> 00:05:28,380 ‫Now we press enter, enter, enter and press se and you see se was pressed. 65 00:05:28,620 --> 00:05:33,750 ‫All right, so now you know how you can manipulate your text elements, how you can use them in general. 66 00:05:33,750 --> 00:05:39,660 ‫Because what we will do is, of course we will use them in the Pong game and in general in every game 67 00:05:39,960 --> 00:05:41,370 ‫because we will need a UI. 68 00:05:41,370 --> 00:05:43,620 ‫And that's for example how you can use text.