Hello guys.
Welcome to the video.
In this one we need to fix one of the bugs we have for a while.
And I've been ignoring it for a while, but it just, you know, I did not have a great chance to fix
it.
But now we have an amazing chance.
Because now I can show you what is, uh, event action and how to use it.
It's a very nice feature that is very convenient, and it can be pretty useful if you use it.
Right.
So first of all, I want to show you a debug that we have on the prefab of a player manager.
I'm going to set respawn delay to 1.5 seconds and let me drop another enemy on the scene.
I'm going to take this chicken and I'm going to place it over here.
So now I'm going to go to play mode.
And once I die, you'll see that we have an error that says that we don't have access to the player
transform over here.
Missing reference exception.
This happens because we try to get access to the player transform on the player manager, and at the
moment when we kill the player, the player is dead and we do not have anything on the player manager
field.
Uh.
Over here.
You can see we have it now.
But if I die, it says missing.
Sorry.
Once again.
You can see it says missing and that's why we see an error.
So we need to fix it.
And there is a very simple and convenient way to fix it.
Now let's go to the scripts.
Enemy chicken.
And we're going to go ahead.
To, uh, Layer manager one second.
Layer manager.
And at the top over here I want to make a public static event action.
And I'm going to name it as on layer respawn.
So now we've made an action and we can invoke it every time we respawn the player.
That's the first step.
Let's go here at the place where we respawn the player.
On player respawn.
Question mark Doh't.
Invoke.
So now this will be invoked every time we respawn the player.
And also now we can subscribe to this event.
And every time it is invoked we can do something else.
It's like we sort of attaching another method to this event.
And every time this event is invoked, we're going to also call that method that is attached to this
event okay.
And I know it's all confusing and maybe have lots of questions, but in general it's just like that.
That's how you use it.
And it's very simple to deal with.
You make an action, then you go and invoke it at the place you want it to invoke.
For example, we want this event to happen every time we respawn the player, just like we did here
and now.
Every time this will be invoked, all of the, uh, methods that we subscribe to this event will be
called.
So let's go to, uh, enemy over here.
And we have this method update player ref.
So what we can do here is go to start.
And we're going to do player manager on player respawn plus equals update players ref just like that.
And maybe let's rename it to Update players update player reference just a little bit better.
Now we don't need to do invoke repeating, because this will be called every time we invoke that event.
And maybe we want to unsubscribe from the event every time enemy dies.
So somewhere here I'm going to do player manager on player respawn minus equals update player reference.
All right.
Very good.
That's all what we need to do.
Now let's go back to unity.
And, uh, we're going to go to play mode.
Q.
Going to get some fruits and we have a player manager reference here.
But if I jump into the gap, I'm going to kill the player.
It's going to say missing, but we do not have any errors in here at the moment.
If we go to debug mode on the enemy chicken.
We can see that player transform.
Says none.
Now let's unpause the game.
I'm going to respond and we can see player chicken now has reference of a player clone transform, which
is this one and same reference we have on the player manager over here.
Isn't it great?
It is great and probably I should do it like that in the first place.
But I didn't because sometimes, you know, like my brain is not working.
So yeah.
Thank you so much for paying attention.
That's all what I wanted to do in this video.
Just a quick example on how to use action events.
And let me know if you want to know more about action events.
Maybe I can make some video for that and I will explain things in details, but as of now, we don't
need to know much about them.
We can just use this simple example because we don't really need other functionality from them.
Okay, well see you in the next video.