Rather than having to manually change the NUM_INVENTORY_SLOTS Integer constant in the PlayerInventoryDisplay script class to match the number of GameObjects that have been created in the scene for the player to collect, let's have our script count how many GameObjects are tagged Star, and then use this to size and populate our array of references to inventory UI panel slots.
We just need to change from a constant to a variable for our array size and set that variable before anything else in our Awake() method. The GameObject.FindGameObjectsWithTag("Star") statement gets an array of references to all GameObjects tagged with Star, and its length is the array size we want:
- Replace the C# PlayerInventoryDisplay script class in the player-SpaceGirl GameObject with the following code:
using UnityEngine; using System.Collections;...