How to do it...

To display multiple pickups of different objects as text totals via a dynamic Dictionary, follow these steps:

  1. Replace the content of the script class PickUp with the following code:
using UnityEngine; 

public class PickUp : MonoBehaviour { 
   public enum PickUpType { 
         Star, Key, Heart 
   } 

   public PickUpType type; 
} 
  1. Remove the instance of the script class PlayerInventory from the GameObject player-SpaceGirl.
  2. Create a new C# script class PlayerController containing the following code, and add an instance as a component to GameObject player-girl1:
using UnityEngine; 

public class PlayerController : MonoBehaviour { 
   private InventoryManager inventoryManager; 

   void Awake() { 
         inventoryManager = GetComponent<InventoryManager>(); 
   } 

   void OnTriggerEnter2D(Collider2D hit) { 
         if(hit.CompareTag("Pickup")){ 

PickUp item = hit.GetComponent<PickUp> ();

               inventoryManager.Add(item); 
               Destroy(hit.gameObject); 
         } 
   } 
} 
  1. Replace the content of script class PlayerInventoryDisplay with the following code:
using UnityEngine; 
using UnityEngine.UI; 
using System.Collections.Generic; 

[RequireComponent(typeof(PlayerController))] 
[RequireComponent(typeof(InventoryManager))] 
public class PlayerInventoryDisplay : MonoBehaviour { 
   public Text inventoryText; 

   public void OnChangeInventory(Dictionary<PickUp.PickUpType, int> inventory) { 
         inventoryText.text = ""; 
         string newInventoryText = "carrying: "; 

         foreach (var item in inventory) { 
               int itemTotal = item.Value; 
               string description = item.Key.ToString(); 
               newInventoryText += " [ " + description + " " + itemTotal + " ]"; 
         } 

         int numItems = inventory.Count; 
         if (numItems < 1) 
               newInventoryText = "(empty inventory)"; 

         inventoryText.text = newInventoryText; 
   } 
} 
  1. Add an instance of the following C# Script InventoryManager to the GameObject player-SpaceGirl in the Hierarchy:
using UnityEngine; 
using System.Collections.Generic; 

public class InventoryManager : MonoBehaviour { 
   private PlayerInventoryDisplay playerInventoryDisplay; 
   private Dictionary<PickUp.PickUpType, int> items = new Dictionary<PickUp.PickUpType, int>(); 

   void Awake() { 
         playerInventoryDisplay = GetComponent<PlayerInventoryDisplay>(); 
   } 

   void Start() { 
         playerInventoryDisplay.OnChangeInventory(items); 
   } 

   public void Add(PickUp pickup) { 
         PickUp.PickUpType type = pickup.type; 
         int oldTotal = 0; 

         if(items.TryGetValue(type, out oldTotal)) 
               items[type] = oldTotal + 1; 
         else 
               items.Add (type, 1); 

         playerInventoryDisplay.OnChangeInventory(items); 
   } 
} 
  1. In the Hierarchy (or Scene) panel, select each pickup GameObject in turn, and choose from the drop-down menu its corresponding Type in the Inspector panel. As you can see, public variables that are of an enum type are automatically restricted to the set of possible values as a combo-box drop-down menu in the Inspector panel.
  1. Play the game. First, you should see a message on screen stating the inventory is empty, and then as you pick up one or more items of each pickup type, you'll see text totals of each type you have collected.