Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by SinkingSun, Jul 3, 2023.

Thread Status:
Not open for further replies.
  1. SinkingSun

    SinkingSun

    Joined:
    Feb 14, 2022
    Posts:
    13
    I gave the player a List<GameObject> that I'm using as an inventory.

    Code (CSharp):
    1. public class InventoryTest : MonoBehaviour
    2. {
    3.     public List<GameObject> inventory = new List<GameObject>();
    4.  
    5.     public void AddToInventory(GameObject other)
    6.     {
    7.         inventory.Add(other);
    8.     }
    9. }
    I also have an InteractableObject script on some rocks I'm picking up in the scene.

    Code (CSharp):
    1. public class InteractableObject : MonoBehaviour
    2. {
    3.  
    4.     public bool playerInRange;
    5.     public string ItemName;
    6.  
    7.     public string GetItemName()
    8.     {
    9.         return ItemName;
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.E) && playerInRange && CompareTag("Rock"))
    15.         {
    16.             InventoryTest inventory = FindObjectOfType<InventoryTest>();
    17.             if (inventory != null)
    18.             {
    19.                 inventory.AddToInventory(gameObject);
    20.                 gameObject.SetActive(false); // Hide or disable the rock after picking it up
    21.             }
    22.  
    23.             //Debug.Log("Item added to Inventory");
    24.             //Destroy(gameObject);
    25.         }
    26.     }
    27.  
    28.     private void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.CompareTag("Player"))
    31.         {
    32.             playerInRange = true;
    33.         }
    34.     }
    35.  
    36.     private void OnTriggerExit(Collider other)
    37.     {
    38.         if (other.CompareTag("Player"))
    39.         {
    40.             playerInRange = false;
    41.         }
    42.     }
    43. }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,714
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    I already responded to you in previous thread, and you've ignored everything I wrote.
    https://forum.unity.com/threads/how-to-script-an-inventory-system.1455913/#post-9120973

    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.
     
    Last edited: Jul 3, 2023
    orionsyndrome and Kurt-Dekker like this.
  4. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106
    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.
     
    Last edited: Jul 3, 2023
  5. SinkingSun

    SinkingSun

    Joined:
    Feb 14, 2022
    Posts:
    13
    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.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Teacher, eh? ROFL.
     
    Kurt-Dekker likes this.
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    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.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    upload_2023-7-4_5-6-19.png
     
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,967
    Duplicate, pointless, low effort. Read rules (and preferably the docs) before posting again. Closed.
     
Thread Status:
Not open for further replies.