1 00:00:04,000 --> 00:00:04,810 ‫Welcome back. 2 00:00:04,840 --> 00:00:11,680 ‫In this video, we are going to have a look at invoke repeating which allows us to do something repeatedly, 3 00:00:11,680 --> 00:00:13,760 ‫every so often, so many seconds. 4 00:00:13,780 --> 00:00:19,090 ‫So let's go ahead and go back to our Instantiate Cubes script that we have created. 5 00:00:19,090 --> 00:00:25,000 ‫And now I want to have a method that creates cubes all the time. 6 00:00:25,000 --> 00:00:33,460 ‫So let's go ahead and create the method, public void, create new cube. 7 00:00:35,500 --> 00:00:44,170 ‫And in here we are simply we simply going to create a new cube at well, pretty much the same position 8 00:00:44,170 --> 00:00:45,520 ‫that we had last time. 9 00:00:46,030 --> 00:00:53,410 ‫So in the key down, each time that a cube is created, we want to instantiate it at a specific position. 10 00:00:53,410 --> 00:00:57,190 ‫So maybe we'll start at not minus five, but at minus ten. 11 00:00:57,730 --> 00:01:03,190 ‫And now instead of having an update method, we are going to use the start method. 12 00:01:03,910 --> 00:01:07,410 ‫And then here I will use invoke repeating. 13 00:01:07,420 --> 00:01:14,110 ‫That's a method that invokes the method with a method name in time seconds then repeatedly every repeat 14 00:01:14,120 --> 00:01:14,860 ‫rate seconds. 15 00:01:14,860 --> 00:01:20,950 ‫So which means it will start after a certain of time that we can set up ourselves to do something, 16 00:01:20,950 --> 00:01:27,040 ‫to call a method and do so repeatedly so we can just say invoke repeating. 17 00:01:27,580 --> 00:01:29,320 ‫And how do we do that? 18 00:01:29,320 --> 00:01:35,830 ‫Well, we give it an F method name, which is create new cube, which is this name here, create new 19 00:01:35,830 --> 00:01:36,130 ‫cube. 20 00:01:36,130 --> 00:01:41,110 ‫If you want to make sure that you spell it correctly, you can simply copy that and enter it here. 21 00:01:41,620 --> 00:01:46,240 ‫Then the next parameter it needs after how many seconds do we want to start? 22 00:01:46,240 --> 00:01:51,760 ‫Let's say after 3 seconds we want to start this and then we have to set the repeat repeat rate. 23 00:01:51,760 --> 00:01:55,030 ‫So after how many seconds should it repeat itself? 24 00:01:55,030 --> 00:01:57,910 ‫So let's say after every second. 25 00:01:58,960 --> 00:02:00,610 ‫So now let's see what happens. 26 00:02:02,080 --> 00:02:03,280 ‫Let's start our game. 27 00:02:04,870 --> 00:02:13,750 ‫And we are after 3 seconds, we see that it starts to create cubes and the cubes are one next to the 28 00:02:13,750 --> 00:02:14,290 ‫other. 29 00:02:15,400 --> 00:02:20,410 ‫Now that will do the same thing over and over again until infinite. 30 00:02:20,410 --> 00:02:27,820 ‫So until there is no more ram left for our game to run, until it crashes because there are too many 31 00:02:27,820 --> 00:02:29,080 ‫game objects in there. 32 00:02:29,080 --> 00:02:31,630 ‫So you have to be careful with invoke repeating. 33 00:02:31,840 --> 00:02:44,320 ‫You can of course invoke cancel at one point so we could say if counter is higher equal 20 or maybe 34 00:02:44,590 --> 00:02:47,950 ‫let's start off with five so that we can see the result of it. 35 00:02:48,640 --> 00:02:53,200 ‫Invoke here, cancel, invoke. 36 00:02:53,560 --> 00:02:57,100 ‫So cancels all invoke calls on this mono behavior. 37 00:02:57,130 --> 00:02:58,630 ‫All right, so let's do that. 38 00:02:58,930 --> 00:02:59,860 ‫Let's save it. 39 00:03:00,130 --> 00:03:02,590 ‫And now let's see how many cubes it will create. 40 00:03:02,590 --> 00:03:04,270 ‫It shouldn't be more than five. 41 00:03:04,270 --> 00:03:05,710 ‫So let's see. 42 00:03:06,220 --> 00:03:10,420 ‫One, two, three, four and five. 43 00:03:10,570 --> 00:03:16,750 ‫All right, so now you know how to start invoke repeating and how to end invoke repeating. 44 00:03:16,750 --> 00:03:19,150 ‫So you can simply cancel, invoke. 45 00:03:19,150 --> 00:03:26,110 ‫By the way, if you want to call something only once, I think it's still an interesting method. 46 00:03:26,410 --> 00:03:32,980 ‫Then you can use invoke and then that's the same thing only without repeating. 47 00:03:32,980 --> 00:03:40,600 ‫So you could, for example, say, we want to create new cube, so let's call this create new cube method 48 00:03:40,840 --> 00:03:42,790 ‫after 5 seconds. 49 00:03:44,350 --> 00:03:48,160 ‫So now let's comment that invoke repeating out. 50 00:03:48,160 --> 00:03:54,280 ‫So the only thing that will happen is after 5 seconds, there will be one single invoke statement that 51 00:03:54,280 --> 00:03:56,410 ‫will call this create new cube. 52 00:03:56,410 --> 00:03:59,110 ‫So you can use that in order to have delays. 53 00:03:59,110 --> 00:04:03,010 ‫So it will delay the creation of a cube by 5 seconds. 54 00:04:03,250 --> 00:04:10,930 ‫So I know this will be silent 5 seconds, but after a while we should see that there is a cube. 55 00:04:12,890 --> 00:04:14,960 ‫And as you can see, there is our cue. 56 00:04:15,770 --> 00:04:16,160 ‫Great. 57 00:04:16,160 --> 00:04:21,860 ‫So now you know how to use invoke repeating how to use, invoke and how to cancel this invoke repeating 58 00:04:21,860 --> 00:04:22,430 ‫method.