My project doesn't have the InventoryUI.cs script

I’m working on the “RPG Inventory Systems” course, and I completed the “InventorySlotUI.cs Changes” lesson, but now when I try to play the game it gives me errors like this:

“Assets\GameDev.tv Assets\Scripts\UI\Inventories\InventorySlotUI.cs(4,17): error CS0234: The type or namespace name ‘Inventories’ does not exist in the namespace ‘GameDevTV’ (are you missing an assembly reference?)”

“Assets\GameDev.tv Assets\Scripts\UI\Inventories\InventorySlotUI.cs(9,66): error CS0246: The type or namespace name ‘InventoryItem’ could not be found (are you missing a using directive or an assembly reference?)”

It tooke me a while, but I found the source of the problem: In the next lesson about using the debugger tool, I discovered that Rick and Sam had at least one script that I didn’t: the InventoryUI.cs script. Curiously enough, I can find it in Visual Studio Code’s explorer, but it claims that the path to it doesn’t exist anymore. I know that I haven’t moved it and it wasn’t included in the builds you download at the start of course, so where do I get it?

I’m not sure why the script wouldn’t be in there, but here’s the script, you can add to your project:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Inventories;
using TMPro;

namespace GameDevTV.UI.Inventories
{
    /// <summary>
    /// To be placed on the root of the inventory UI. Handles spawning all the
    /// inventory slot prefabs.
    /// </summary>
    public class InventoryUI : MonoBehaviour
    {
        // CONFIG DATA
        [SerializeField] InventorySlotUI InventoryItemPrefab = null;
        [SerializeField] bool isPlayerInventory = true;
        [SerializeField] TextMeshProUGUI Title;

        // CACHE
        Inventory selectedInventory;

        // LIFECYCLE METHODS

        private void Awake() 
        {
            if (isPlayerInventory)
            {
                selectedInventory = Inventory.GetPlayerInventory();
                selectedInventory.inventoryUpdated += Redraw; 
            }
        }

        private void Start()
        {
            if(isPlayerInventory)
            {
                Redraw();
            }
        }

        public bool Setup(GameObject user)
        {
            if (user.TryGetComponent(out selectedInventory))
            {
                selectedInventory.inventoryUpdated += Redraw;
                Title.text = selectedInventory.GetDisplayName();
                Redraw();
                return true;
            }
            Debug.Log($"{name} does not have an inventory attached.");
            return false;
        }

        // PRIVATE

        private void Redraw()
        {
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }

            for (int i = 0; i < selectedInventory.GetSize(); i++)
            {
                var itemUI = Instantiate(InventoryItemPrefab, transform);
                itemUI.Setup(selectedInventory, i);
            }
        }
    }
}

There we go, now my project is working fine. Thanks for the response!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms