0
\$\begingroup\$

I am trying to make an inventory system for the game I'm working on with Unity and I want it to expand and compress whenever objects are added or removed. Is there any go-to way to do this?

\$\endgroup\$
  • \$\begingroup\$ Hi dear friend welcome to gamedev stack exchange.did you search in google before asking this question? there are many tutorials that learn you how you can make Inventory.but if you have specific problem in Implementing inventory share code and tell me what is your problem. \$\endgroup\$ – Seyed Morteza Kamali Dec 24 '18 at 10:51
  • \$\begingroup\$ theres many many different ways. You could store everything you are holding as purely a string in a big long string, but that is likely not particularly useful. I'm currently trying to make a decent inventory myself. You have to make a class called InventoryItem, then that can hold any number and types of fields of data you like (regarding what makes an inventory item eg. Sprite, Name, valueInGold, MagicValue etc etc) . Then on your player class for example you can make an Array of InventoryItem[]. Hope that helps get you started! (NOTE. I'd actually use List<> rather than Array, because... \$\endgroup\$ – Big T Larrity Dec 24 '18 at 13:06
  • \$\begingroup\$ ...List<> can be a dynamic size (you don't need to specify the size in advance when you initialise the List, but with array you would need to initialise it with a fixed capactiy \$\endgroup\$ – Big T Larrity Dec 24 '18 at 13:11
  • \$\begingroup\$ @BigTLarrity please avoid answering questions in the comments. If you feel a List<T> is a good solution here, share it as an answer. And Miya: can you clarify what you've tried so far and what part of the problem you're stuck on? There's a lot that could potentially go into an inventory system, so to be sure we're giving you what you need it helps to describe your situation in detail. \$\endgroup\$ – DMGregory Dec 27 '18 at 17:42
  • \$\begingroup\$ Hi DMGregory , I did have it answered, but then I realised my answer might not have worked so I deleted it. ..I'll undelete it and get rid of the wrong part. \$\endgroup\$ – Big T Larrity Dec 27 '18 at 18:10
0
\$\begingroup\$

A very basic version to get started (note I'm still learning myself, but I happen to be several weeks into making my own games with inventories).

You can make a class such as :

using UnityEngine;
using UnityEngine.UI;

public class InventoryItem : Monobehaviour
{
    // you could for example attach this script to any object that can be picked up, 
    // and then assign values to these fields in the Unity Inspector...
    public string uiTextName;
    public int saleValue;
    public int magicValue;
    public int healthValue;

    public Player player; // note it is better to keep track of the player in a GameController type script which will hold all the game info such as the current player, when to spawn enemies, make game over, and other things. But for now you could add it here using inspector.

    // check if mouse is pressed on this object, find the player
    // and add the item to its inventory:
    private void OnMouseDown()
    {

         player.AddToInventory(item); // we make this class and function in a second, below.
         // ... perhaps here you can un-enable the MeshRenderer and Collider of this Item so it vanishes in the scene...
         MeshRenderer m = GetComponent<MeshRenderer>();
         Collider c = GetComponent<Collider>();
         if (m != null)
              m.enabled = false;
         if (c != null)
              c.enabled = false;
    }
}

then make a Player class:

using UnityEngine;
using System.Collections.Generic; // this is needed to make us of c# List<>

public class Player : Monobehaviour
{
     // declare a List<> to hold all InventoryItems we collect.
     List<InventoryItem> playersInventory = new List<InventoryItem>();


     // this public function can be called to 'Add' and 'InventoryItem' to the players inventory....
     public void AddItemToInventory(InventoryItem item)
     {
          playersInventory.Add(item);
     }


}

I hope that makes sense to you and helps you along the way, if you need more help let me know as I only recently have been learning this myself.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.