Displaying multiple pickups of different objects as text totals via a dynamic Dictionary<> of PickUp objects and enum pickup types

While the previous recipe worked fine, any old text might have been typed into the description for a pickup or perhaps mistyped (star, Sstar, starr, and so on). A much better way of restricting game properties to one of a predefined (enumerated) list of possible values is to use C# enums. As well as removing the chance of mistyping a string, it also means that we can write code to appropriately deal with the predefined set of possible values. In this recipe, we will improve our general purpose PickUp class by introducing three possible pickup types (Star, Heart, and Key), and write inventory display code that counts the number of each type of pickup being carried and displays these totals via a UI Text object on screen. We also switch from using a List to using a Dictionary, since the Dictionary data structure is designed specifically for key-value pairs, perfect for associating a numeric total with an enumerated pickup type.

In this recipe, we will also manage the additional complexity by separating the controller (user collection event) logic from the stored inventory data, by introducing an inventory manager scripted class. Our player controller is then simplified to just two methods (Awake, getting a reference to the inventory manager, and OnTriggerEnter2D, responding to collisions by communicating with the inventory manager).