‫Welcome back.
‫In this video, we are going to have a look at invoke repeating which allows us to do something repeatedly,
‫every so often, so many seconds.
‫So let's go ahead and go back to our Instantiate Cubes script that we have created.
‫And now I want to have a method that creates cubes all the time.
‫So let's go ahead and create the method, public void, create new cube.
‫And in here we are simply we simply going to create a new cube at well, pretty much the same position
‫that we had last time.
‫So in the key down, each time that a cube is created, we want to instantiate it at a specific position.
‫So maybe we'll start at not minus five, but at minus ten.
‫And now instead of having an update method, we are going to use the start method.
‫And then here I will use invoke repeating.
‫That's a method that invokes the method with a method name in time seconds then repeatedly every repeat
‫rate seconds.
‫So which means it will start after a certain of time that we can set up ourselves to do something,
‫to call a method and do so repeatedly so we can just say invoke repeating.
‫And how do we do that?
‫Well, we give it an F method name, which is create new cube, which is this name here, create new
‫cube.
‫If you want to make sure that you spell it correctly, you can simply copy that and enter it here.
‫Then the next parameter it needs after how many seconds do we want to start?
‫Let's say after 3 seconds we want to start this and then we have to set the repeat repeat rate.
‫So after how many seconds should it repeat itself?
‫So let's say after every second.
‫So now let's see what happens.
‫Let's start our game.
‫And we are after 3 seconds, we see that it starts to create cubes and the cubes are one next to the
‫other.
‫Now that will do the same thing over and over again until infinite.
‫So until there is no more ram left for our game to run, until it crashes because there are too many
‫game objects in there.
‫So you have to be careful with invoke repeating.
‫You can of course invoke cancel at one point so we could say if counter is higher equal 20 or maybe
‫let's start off with five so that we can see the result of it.
‫Invoke here, cancel, invoke.
‫So cancels all invoke calls on this mono behavior.
‫All right, so let's do that.
‫Let's save it.
‫And now let's see how many cubes it will create.
‫It shouldn't be more than five.
‫So let's see.
‫One, two, three, four and five.
‫All right, so now you know how to start invoke repeating and how to end invoke repeating.
‫So you can simply cancel, invoke.
‫By the way, if you want to call something only once, I think it's still an interesting method.
‫Then you can use invoke and then that's the same thing only without repeating.
‫So you could, for example, say, we want to create new cube, so let's call this create new cube method
‫after 5 seconds.
‫So now let's comment that invoke repeating out.
‫So the only thing that will happen is after 5 seconds, there will be one single invoke statement that
‫will call this create new cube.
‫So you can use that in order to have delays.
‫So it will delay the creation of a cube by 5 seconds.
‫So I know this will be silent 5 seconds, but after a while we should see that there is a cube.
‫And as you can see, there is our cue.
‫Great.
‫So now you know how to use invoke repeating how to use, invoke and how to cancel this invoke repeating
‫method.