In this lecture, we'll refactor our teddy bear destruction game from the previous module to use an event and an event handler. The event manager class that we implement in this solution is even more robust than the one that we implemented in the previous lecture, because it supports multiple events, invokers and multiple listeners. Let's go take a look. This is our teddy bear destruction game from the previous module. So when I run it, you can see that I can mouse over teddy bears and I get score as I mouse over them and so on. In this particular implementation, we don't have a HUD. We have our game manager teddy bear destruction script instead. So for the scoring part, our teddy bear destruction script keeps track of the score, and displays it as appropriate, and it exposes an AddPoints method. And if I right-click this and say Find References, I can see that the DisappearingTeddyBear, the BurningTeddyBear and the ExplodingTeddyBear all call that method to AddPoint when those teddy bears get destroyed. So we're going to refactor this code to use the UnityEvent, and UnityAction and an EventManager. So we've implemented a points added event, that looks remarkably similar to the event that we implemented in our fish game. So the event manager is more robust than the event manager in our fish game, in part because we now have a list of invokers, because every teddy bear can invoke the points added event when that teddy bear is destroyed. So, in the previous game, it was only the fish that could have invoked that event, but now we have a list of invokers. Similarly, we have a list of listeners, we actually realize that the HUD is the only object that will be a listener in this particular implementation, but it's more robust to allow multiple listeners, so I've provided that capability here. The AddInvoker method looks similar to what we had in the fish game. However, this time when we pass an invoker, we add it to the list of invokers for the game. And because we might have listeners hanging around, we walk through that list of listeners and add each of them as a listener to the new invoker. Now we don't have to check for null like we did before, because if we don't have any listeners added yet, the listeners list is empty. So this foreach loop doesn't actually execute the body at all, because there are no elements in the list. Similarly, if we add a listener, we are going to add that listener to the list of listeners, but there might also be a bunch of invokers already hanging around. And so we're going to walk through the list of invokers, adding this new list listener to each of those invokers. So this event manager is more robust than the previous one because we can have multiple invokers. And we end up adding invokers every time we spawn a teddy bear. So let's actually see how the HUD and teddy bears deals with this. The HUD works essentially the same way it did in the fish game. So in the Start method, it calls the EventManager.AddListener method passing in the method it wants to use to handle that event. I've changed the name of that method, so now it's called HandlePointsAddedEvent, but it doesn't really matter the name, right? It is in fact a delegate that matches UnityAction with one integer parameter. So the name doesn't matter. I happen to use HandlePointsAddedEvent here. So it adds to the points and updates the text just as you'd expect. The TeddyBear class, remember the root of our TeddyBear class hierarchy has a new field called PointsAddedEvent. In the Start method it adds itself as an invoker of that event. And notice that this field is protected, because each of the child classes needs to be able to access this event to invoke it. So let's look at DisappearingTeddyBear, and as you can see in the ProcessMouseOver method, it accesses that pointsAddedEvent to call the Invoke method on it with the pointValue for this particular TeddyBear. So the ExplodingTeddyBear and the BurningTeddyBear work in the same way invoking this event. So, firing off this event. So again, the TeddyBear class doesn't have to know about the HUD class, and the HUD doesn't have to know about the TeddyBear class. Those classes just use the EventManager to connect the invokers and the listeners for the pointsAddedEvent. To recap, the EventManager that we implemented in this lecture is even more robust, because it supports multiple invokers and multiple listeners for an event.