1 00:00:04,190 --> 00:00:05,120 ‫We'll come back. 2 00:00:05,150 --> 00:00:11,540 ‫In this video, we will have a look at our new behavior script, which is the script that we have created 3 00:00:11,540 --> 00:00:12,590 ‫in the last video. 4 00:00:12,620 --> 00:00:18,680 ‫If you want to create a script yourself, you can do right click in your assets, create and click on 5 00:00:18,680 --> 00:00:19,760 ‫C-sharp script. 6 00:00:19,880 --> 00:00:24,830 ‫Another way to create a script is, for example, click on one of your game objects, for example, 7 00:00:24,830 --> 00:00:25,520 ‫the cube. 8 00:00:25,700 --> 00:00:27,680 ‫Then add a component. 9 00:00:27,920 --> 00:00:30,860 ‫Here on the right hand side, you can create a new script. 10 00:00:31,160 --> 00:00:36,050 ‫You can add a name here, new behavior script, and you can choose the language here. 11 00:00:36,050 --> 00:00:42,110 ‫So if you want to program in JavaScript, you could choose that here as well, and then simply create 12 00:00:42,110 --> 00:00:45,020 ‫and add and it will be added to your assets folder. 13 00:00:45,560 --> 00:00:48,860 ‫But I'm not going to do it this way because I have the script already. 14 00:00:48,860 --> 00:00:50,690 ‫So let's open up that script. 15 00:00:50,690 --> 00:00:58,130 ‫And usually if we double click it, it opens in our IDE, for example, in Visual Studio, and it could 16 00:00:58,130 --> 00:01:05,900 ‫also open up in mono develop if you if you use that or in any other IDE that supports C-sharp and Unity. 17 00:01:05,900 --> 00:01:08,870 ‫I highly recommend, however, to use Visual Studio. 18 00:01:09,320 --> 00:01:09,710 ‫All right. 19 00:01:09,710 --> 00:01:15,050 ‫So let's have a look at this new behavior script because we have already 16 lines of code, even though 20 00:01:15,050 --> 00:01:19,760 ‫we haven't written any single line or even any single symbol yet. 21 00:01:19,760 --> 00:01:26,840 ‫So what we have here is using we have three lines of using something, which means that we are using 22 00:01:26,840 --> 00:01:27,950 ‫a namespace. 23 00:01:28,100 --> 00:01:34,760 ‫You can consider those or you can imagine those being modules which add a bunch of functionality to 24 00:01:34,760 --> 00:01:39,740 ‫your script and those allow us to use this functionality. 25 00:01:39,740 --> 00:01:44,630 ‫So we're not using it currently because we have not used any of those too. 26 00:01:44,750 --> 00:01:51,290 ‫As you can see, they are grayed out, but we are using some of the unity engine functionality and we 27 00:01:51,290 --> 00:01:58,520 ‫do that within mono behavior because mono behavior is one of the classes that is within Unity Engine. 28 00:01:59,000 --> 00:02:04,520 ‫If you want to know more about mono behavior, what you can do is hold your command key and press on 29 00:02:04,520 --> 00:02:05,570 ‫mono behavior. 30 00:02:05,600 --> 00:02:12,020 ‫What that does, it opens up the assembly browser and then you will find here the information about 31 00:02:12,020 --> 00:02:18,350 ‫this class that has plenty of code that might be a bit too much for you as a beginner, but if you know 32 00:02:18,350 --> 00:02:20,900 ‫programming already, you might make sense out of it. 33 00:02:20,960 --> 00:02:27,530 ‫In the end, there are plenty of methods which we can use in our games or in our scripts in general. 34 00:02:27,920 --> 00:02:28,370 ‫All right. 35 00:02:28,370 --> 00:02:29,450 ‫So what do we have here? 36 00:02:29,450 --> 00:02:35,030 ‫We have a public class called New Behavior Script Colon, mono behavior. 37 00:02:35,060 --> 00:02:42,140 ‫This colon means that we are inheriting from mono behavior, which means that we can use the functionality 38 00:02:42,140 --> 00:02:43,130 ‫of mono behavior. 39 00:02:43,130 --> 00:02:48,020 ‫And we have the behavior of this class, and this class is mono behavior. 40 00:02:48,620 --> 00:02:55,070 ‫So within those things there is somewhere the start and the update method which are already there. 41 00:02:55,250 --> 00:03:00,890 ‫So we don't need to write exactly when the start method or the update method is called. 42 00:03:00,920 --> 00:03:04,040 ‫It's all done within our unity engine namespace. 43 00:03:04,040 --> 00:03:06,050 ‫So all of that is written there. 44 00:03:06,050 --> 00:03:07,640 ‫We don't need to write that code. 45 00:03:07,820 --> 00:03:10,430 ‫So now that allows us to make it super simple. 46 00:03:10,430 --> 00:03:14,990 ‫We can simply say, All right, we want to do something when the start method is called, then we want 47 00:03:14,990 --> 00:03:17,360 ‫to do something whenever the update method is called. 48 00:03:17,630 --> 00:03:23,360 ‫So those two methods, they are within a class and that's generally how classes are built. 49 00:03:23,360 --> 00:03:29,540 ‫They have variables and they have methods and we can create our own variables. 50 00:03:29,540 --> 00:03:35,420 ‫We will do that in the next video and we can create our own methods, or we could use methods that are 51 00:03:35,420 --> 00:03:37,100 ‫internally created already. 52 00:03:37,100 --> 00:03:42,950 ‫So we don't need to build them ourselves, we just need to call them or we can override them to make 53 00:03:42,950 --> 00:03:43,910 ‫our own of it. 54 00:03:44,090 --> 00:03:48,590 ‫But all of that is going to be covered as soon as we get there. 55 00:03:48,590 --> 00:03:54,590 ‫What is important for now is that, you know, that we automatically create a class and that has automatically 56 00:03:54,590 --> 00:03:58,790 ‫two methods which we could choose to delete or we can choose to use them. 57 00:03:58,940 --> 00:04:01,190 ‫So let's have a quick look what those to do. 58 00:04:01,190 --> 00:04:06,830 ‫For example, let's write, we have print statement onto our console. 59 00:04:06,830 --> 00:04:13,880 ‫So for example, we say start method was called 60 00:04:16,550 --> 00:04:18,860 ‫and in the update method we do the same thing. 61 00:04:18,860 --> 00:04:24,080 ‫Print update method was called. 62 00:04:24,080 --> 00:04:31,670 ‫So now we will know within unity within our console, we will get information about when what was called. 63 00:04:31,670 --> 00:04:35,780 ‫So which point was the start method called and when was the update method called? 64 00:04:35,780 --> 00:04:37,700 ‫What do those two methods do? 65 00:04:37,730 --> 00:04:44,750 ‫The START method is called at the beginning, when the game object that contains our new behavior script 66 00:04:45,500 --> 00:04:50,540 ‫is created and the update method is then called with every single frame. 67 00:04:50,540 --> 00:04:56,270 ‫So as soon as the object is there, the update method will be called all the time, once per frame, 68 00:04:56,270 --> 00:05:02,180 ‫which means if we have, for example, 60 frames per second, then this update method will be called 69 00:05:02,180 --> 00:05:03,320 ‫60 times. 70 00:05:03,610 --> 00:05:04,900 ‫Within that second. 71 00:05:05,740 --> 00:05:06,040 ‫All right. 72 00:05:06,040 --> 00:05:08,550 ‫Let's check that out before we go any further. 73 00:05:08,560 --> 00:05:10,270 ‫So let's save that script. 74 00:05:11,170 --> 00:05:12,910 ‫And now let's go to Unity. 75 00:05:13,060 --> 00:05:18,310 ‫If we run the code now like that, there will be nothing printed onto our console. 76 00:05:18,400 --> 00:05:23,320 ‫As you can see, maybe it's best if I don't go to maximize on play here. 77 00:05:23,890 --> 00:05:25,840 ‫So let's run that again. 78 00:05:26,440 --> 00:05:29,620 ‫Go to our console and you can see nothing happens. 79 00:05:29,620 --> 00:05:37,420 ‫So no information was printed if we now, for example, add that new behavior script to our cube. 80 00:05:39,060 --> 00:05:41,170 ‫Then we can see it here on the right hand side. 81 00:05:41,190 --> 00:05:48,450 ‫So I clicked on the cube and in the inspector on the right hand side, I can find my new behavior script. 82 00:05:49,050 --> 00:05:55,470 ‫And for example, I could remove it and I can alternatively drag it in like that. 83 00:05:55,770 --> 00:05:58,320 ‫So I added it like with a drag. 84 00:05:59,010 --> 00:06:00,420 ‫Remove that component again. 85 00:06:00,420 --> 00:06:08,170 ‫And the third way is to add a component, then go to scripts and then add this new behavior script. 86 00:06:08,190 --> 00:06:10,930 ‫So now our cube has the script. 87 00:06:10,950 --> 00:06:14,790 ‫If we run the code now, we will see a different story here. 88 00:06:14,790 --> 00:06:21,750 ‫So let's go to our console and you can see start method was called has a one here at the at the end 89 00:06:21,750 --> 00:06:28,560 ‫which means it is called once and update method was called has already been called 500 times. 90 00:06:28,560 --> 00:06:32,460 ‫So that means it has been called with every single frame. 91 00:06:32,460 --> 00:06:38,670 ‫So as you can see, well, maybe you can roughly estimate how many frames per second I have should be 92 00:06:38,670 --> 00:06:40,680 ‫like 30 or 50 or something like that. 93 00:06:40,980 --> 00:06:45,750 ‫So it's called very many times and that's what you have to consider. 94 00:06:45,750 --> 00:06:52,410 ‫So whenever you write your code within the update method should not be any heavy code that will load 95 00:06:52,410 --> 00:06:58,320 ‫many, many information or will execute a lot of different functionality because otherwise that could 96 00:06:58,320 --> 00:07:00,600 ‫reduce the performance of your game. 97 00:07:00,600 --> 00:07:08,460 ‫So keep the update method lightweight and use the START method for initialization. 98 00:07:08,580 --> 00:07:12,630 ‫What that means will make make a lot more sense when we go on with the course. 99 00:07:12,630 --> 00:07:16,110 ‫So no worries if you don't understand what initialization means. 100 00:07:16,140 --> 00:07:19,890 ‫No worries if you don't understand what this print statement means. 101 00:07:19,890 --> 00:07:23,910 ‫Well, in the end, it's really just for programming services. 102 00:07:24,000 --> 00:07:30,870 ‫It's really just for us so that we know that, so that we can see within unity what's going on. 103 00:07:30,870 --> 00:07:33,600 ‫So that's what we've just written for ourselves. 104 00:07:33,630 --> 00:07:38,940 ‫So we can, for example, check what value our player has. 105 00:07:38,940 --> 00:07:43,800 ‫So for example, how many points he has or whether he tapped a button or those kind of things. 106 00:07:43,800 --> 00:07:45,420 ‫That's what we can use this for. 107 00:07:45,660 --> 00:07:46,980 ‫Two more things. 108 00:07:46,980 --> 00:07:53,670 ‫What we haven't covered yet is the two forward slashes, double forward slash means this line is a comment. 109 00:07:53,670 --> 00:08:00,210 ‫So everything that follows this double slash will not be considered within the game. 110 00:08:00,540 --> 00:08:03,210 ‫It will not be executed, it will not do anything. 111 00:08:03,210 --> 00:08:08,130 ‫It's really just for us in code so that we can read our code in a better way. 112 00:08:08,130 --> 00:08:09,210 ‫So now we understand. 113 00:08:09,210 --> 00:08:12,540 ‫Okay, the start method is for initialization. 114 00:08:12,540 --> 00:08:14,370 ‫The update method is called once per frame. 115 00:08:14,370 --> 00:08:15,510 ‫Alright, very helpful. 116 00:08:16,080 --> 00:08:16,620 ‫All right. 117 00:08:16,620 --> 00:08:21,720 ‫So then the next thing is we have curly brackets as the start and the end of something. 118 00:08:21,720 --> 00:08:26,190 ‫So this is the start of our class and this is the end of our class. 119 00:08:26,280 --> 00:08:27,660 ‫The same thing with the start method. 120 00:08:27,660 --> 00:08:35,340 ‫This is the start of the method curly bracket open and this is the end of our method curly bracket closes. 121 00:08:35,760 --> 00:08:42,270 ‫And of course after every single statement, for example, this print statement, we need to have our 122 00:08:42,270 --> 00:08:43,230 ‫semi-colon. 123 00:08:43,680 --> 00:08:45,240 ‫All right, great. 124 00:08:45,240 --> 00:08:49,500 ‫So these are some very basic things I just wanted to show you. 125 00:08:49,500 --> 00:08:53,310 ‫And in the next video, we are going to have a look at variables. 126 00:08:53,310 --> 00:08:57,420 ‫And if you haven't understood everything you saw here, no worries, you'll get a better feeling for 127 00:08:57,420 --> 00:08:57,990 ‫it later on.