In this recipe, we took one of the panels that contained the Knob circle background, as well as the children GameObjects of the gray and colored star images, and used it to create a prefab called panel-inventory-slot. We then removed the Panel-stars GameObjects from the scene since our PlayerInventoryDisplay script class will create as many of these as needed when the scene begins. This approach saves a lot of dragging and dropping, saving design-time effort, and also eliminating one possible source of sequence/object reference errors when the design of a scene is changed.
The C# PlayerInventoryDisplay script class has two properties:
- A constant integer (NUM_INVENTORY_SLOTS) for defining the number of slots in our inventory, which we set to 5 for this game.
- A (slots) array of references to PickupUI scripted components. Each of these will become a reference to the scripted component in each of the five Panel-inventory-slot GameObjects...