What is the next step for building an inventory now that I have this?

I gave the player a List that I’m using as an inventory.

public class InventoryTest : MonoBehaviour
{
    public List<GameObject> inventory = new List<GameObject>();

    public void AddToInventory(GameObject other)
    {
        inventory.Add(other);
    }
}

I also have an InteractableObject script on some rocks I’m picking up in the scene.

public class InteractableObject : MonoBehaviour
{

    public bool playerInRange;
    public string ItemName;

    public string GetItemName()
    {
        return ItemName;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && playerInRange && CompareTag("Rock"))
        {
            InventoryTest inventory = FindObjectOfType<InventoryTest>();
            if (inventory != null)
            {
                inventory.AddToInventory(gameObject);
                gameObject.SetActive(false); // Hide or disable the rock after picking it up
            }

            //Debug.Log("Item added to Inventory");
            //Destroy(gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = false;
        }
    }
}

What I am getting is the expected result; my player now has an “Inventory” with a few rocks in it, and the objects are set inactive in the hierarchy.

What is the best next step for me in coding my inventory? I do not have any UI for it yet.

  • Should I try to throw those items back out?
  • Do I make something with them?

What do you think I should do as a logical next step and how should it be done? I prefer baby steps if at all possible.

It would be completely and totally dependent on your design.

I already posted you my notes:

I already responded to you in previous thread, and you’ve ignored everything I wrote.

Inventory objects should not be game objects, that’s wrong. Take your example, make things stack, see what happens.

You need to learn what model-view-controller architecture is. That’s the next step. As mentioned in yet another thread , to make the concept click, implement a chess or checkers board that is displayed simultaneously as 2d and 3d view, where you can interact with both views. That may be sufficient for the concept to click.

2 Likes

Yeah, that was literally my first thought when I saw the code above. If an inventory item is a GameObject, then you’re already off to a very rocky start IMO.

An inventory item should really just consist of behind-the-scenes data that can then be utilized depending on the circumstances. In addition to the obvious stuff (item name, price, weight, etc.) the data could also contain a reference to the item’s physical in-game representation (likely a prefab with a model/sprite), UI representation (likely a small icon), etc. That’s why you’ll often see ScriptableObjects used for inventory items–it makes it easy to store all that info in one place that’s accessible from any scene at any time.

I have already changed my game to use scriptable objects instead. You all not helping at all. You’re just trying to tear me down and I’m just getting further and further ahead of you. I’m only sorry I put myself on your radar because you’ll never stop thinking about me. You’re all the same.

Teacher, eh? ROFL.

1 Like

Hire someone to do your job for you if advice makes you upset, don’t forget to pay. The talk about “getting ahead” is laughable. It is not a competition, and some folks here were at it for 20 years or more.

Regarding your scenario, consider following situations:

You pick a rock, and stuff it into inventory. However the rock is a picture in inventory, and a mesh in the world. Where does the picture come from?
You pick a second rock and rocks stack. There are two items, only one image in inventory, with a number. Where does second rock go? Where is the number stored?
You pick an armor which looks like a pile of clothes, when dumped in the world. However, when it is put into armor slot, it alters player’s appearance. Where does the alternative appearance come from?

Ponder those situation. Also, think about your behavior and read “how to ask questions smart way” and SSCCE.

3 Likes

9122443--1265959--upload_2023-7-4_5-6-19.png

Duplicate, pointless, low effort. Read rules (and preferably the docs) before posting again. Closed.