Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by kasuyagi · Apr 30, 2019 at 10:02 AM · inventoryinventory systemitem pickup

My Inventory system is in a big mess now, help me, please

I'm currently creating inventory system for my game. the general concept is, the inventory has room for 5 items and its UI will always be at the bottom of the screen, so no enable/disable function, it's always there. when the player picks an item, it goes to the inventory, first slot first. and if the player pick up another item and the first slot is occupied, the new item goes to the second slot.


when there is an item in a slot, if you press a number key(Alpha1-Alpha5 for Slot0-Slot4) corresponding to the slot, it will make the slot goes "active". for example, if you have an axe in the first slot and you press 1, slot0 will be now "active", thus sending the axe to the player's hand via EquipmentManager script.(from this point in the game, the player will be seen holding an axe). the axe will still stay in the slot though. and when press 1 again, Slot0 will goes "inactive", the axe is no longer equipped in the player's hand. If there is another item like a sword in Slot1 and you press 2, items will be switched, the axe goes inactive(into its slot, the Slot0) and the sword goes into the hand instead.


every item in the inventory will have its fixed slot position until it is dropped off, then the item variable "inWhichSlot" will reset to 0.


the problem is, first of all, when the game is started, there'll always telling me that "more than one instance of Inventory is detected" and the InventorySlot[] array in the InventoryUI script always detect 10 slot components(Slot0-Slot9, there should be only 5 of them) and when i picked up an item, the item goes to the first slot(Slot0) normally and then it cleared out Slot1-Slot9, leaving no icon on the screen at all, there is nothing shown on the ItemList<> or currentItemInInventory[] array in the Inventory script, they're both shown as blanks.


when I press Alpha1 key to get Slot0 activated, the item in the slot duplicated itself until it fills all the slot, the item's icon appeared on the Slot0 and Slot1's UI(only two of the five slots) and if I get to pick up another item, it will say that the Inventory is full. in the debug.log, it's furiously Equip and Unequip the item in a loop endlessly.


this is basically what happened with my code now, it's a total mess. hope that anyone has the solution to this hideous situation, haha. if you need my Unity file or my Script files you can tell me.


Thank you so much in advance for anyone answering and also I have to apologize in advance, too. for my code. It's going to cause a lot of headache for sure. I'm very much in desperation now, but not giving up


here is my code


public class Inventory : MonoBehaviour { #region Singleton public static Inventory instance;

 private void Awake()
 {
     if (instance != null)
     {
         Debug.LogWarning("More than one instance of inventory founded ");
         return;
     }
     instance = this;
 }
 #endregion

 public delegate void OnItemChanged();
 public OnItemChanged onItemChangedCallback;

 public int space=5;

 public List<Items> itemList = new List<Items>();

 public Items[] currentItemInInventory;

 private void Start()
 {
     currentItemInInventory = new Items[space];
 }

 public bool Add(Items item,int slotIndex)
 { 
         if (itemList.Count >= space)
         {
             Debug.Log("Not enough room");
             return false;
         }
        itemList.Add(item);

         currentItemInInventory[slotIndex] = item;
         item.inWhichSlot = slotIndex;
     

         //this code updates the inventory
         if (onItemChangedCallback != null)
         {
             onItemChangedCallback.Invoke();
         }
         return true;
     
 }
 public void Remove(Items item,int slotIndex)
 {
     Debug.Log("Removing item from the list");
     itemList.Remove(item);

     item.inWhichSlot = 0;
     currentItemInInventory[slotIndex] = null;

     if (onItemChangedCallback != null)
     {
         onItemChangedCallback.Invoke();
     }
 }
 

}

public class InventoryUI : PlayerUIScript { public Transform slotHolder; Inventory inventory;

 public InventorySlot[] slots;

 void Start()
 {
     inventory = Inventory.instance;
     inventory.onItemChangedCallback += UpdateUI;

     slots = slotHolder.GetComponentsInChildren<InventorySlot>();
     Debug.Log("there are " + slots.Length + " slot components detected");
 }
 void UpdateUI()
 {
     Debug.Log("UPDATING UI");
     for (int i = 0; i < slots.Length; i++)
     {
         if (i < inventory.itemList.Count)
         {
             slots[i].AddItem(inventory.itemList[i]);
             Debug.Log("item " + inventory.itemList[i] + " added into inventory No."+i);
         }
         else
         {
             slots[i].ClearSlot();
             Debug.Log("Clearing slot No."+i);
         }
     }
 }

 private void Update()
 {
     SlotKeyActivation();
 }

 private void SlotKeyActivation()
 {//this code basically make the slot goes activated when pressed the Alpha number corresponding to the slot and also deactivated other slots if they're previously activated, pretty embarrased by its clunkiness
     if (Input.GetKeyDown(KeyCode.Alpha1))
     {
         slotOn1 = !slotOn1;
         //these code a little bit clunky
         if (slotOn1 == true)
         {
             slotOn2 = false;
             slotOn3 = false;
             slotOn4 = false;
             slotOn5 = false;
         }
     }
     if (Input.GetKeyDown(KeyCode.Alpha2))
     {
         slotOn2 = !slotOn2;
         if (slotOn2 == true)
         {
             slotOn1 = false;
             slotOn3 = false;
             slotOn4 = false;
             slotOn5 = false;
         }
     }
     if (Input.GetKeyDown(KeyCode.Alpha3))
     {
         slotOn3 = !slotOn3;
         if (slotOn3 == true)
         {
             slotOn1 = false;
             slotOn2 = false;
             slotOn4 = false;
             slotOn5 = false;
         }
     }
     if (Input.GetKeyDown(KeyCode.Alpha4))
     {
         slotOn4 = !slotOn4;
         if (slotOn4 == true)
         {
             slotOn1 = false;
             slotOn2 = false;
             slotOn3 = false;
             slotOn5 = false;
         }
     }
     if (Input.GetKeyDown(KeyCode.Alpha5))
     {
         slotOn5 = !slotOn5;
         if (slotOn5 == true)
         {
             slotOn1 = false;
             slotOn2 = false;
             slotOn3 = false;
             slotOn4 = false;
         }
     }
     if (Input.GetKeyDown(KeyCode.X))
     {
         slotOn1 = false;
         slotOn2 = false;
         slotOn3 = false;
         slotOn4 = false;
         slotOn5 = false;
     }
 }

}

public class EquipmentManager : MonoBehaviour { //this script controls which item should be on the player's hand #region Singleton public static EquipmentManager instance; void Awake() { instance = this; } #endregion Items currentEquipment; Inventory inventory; public Transform grab;

 public delegate void OnEquipmentChanged(Items itemInHand, Items oldItem);
 public OnEquipmentChanged onEquipmentChanged;

 void Start()
 {
     inventory = Inventory.instance;
 }
 
 public void Equip(Items itemInHand)
 {
     //means get the item to hand

     Items oldItem = null;
     if (currentEquipment != null)
     {
         oldItem = currentEquipment;
         inventory.Add(oldItem,oldItem.inWhichSlot);
     }
     currentEquipment = itemInHand;

     if (onEquipmentChanged != null)
     {
         onEquipmentChanged.Invoke(itemInHand, oldItem);
     }

     Debug.Log("item has been placed on hand");
 }

 public void Unequip()
 {
  
     if (currentEquipment != null)
     {
         Debug.Log("UnEqipping " + currentEquipment);
         Items oldItem = currentEquipment;
         inventory.Add(oldItem,oldItem.inWhichSlot);

         currentEquipment = null;

         if (onEquipmentChanged != null)
         {
             onEquipmentChanged.Invoke(null, oldItem);
         }
     }
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.X))
     {
         Unequip();
     }
 }

}

public class InventorySlot : MonoBehaviour { // this is a script for each slots, I have five of them in each slot gameObject

 EquipmentManager equipManager;
 public Image icon;
 public Items itemInDaSlot;
 public GameObject SlotYellow;
 public bool thisSlotIsActive = false;
 public int slotIndex;

 private void Start()
 {
    equipManager = EquipmentManager.instance;

 }

 public void AddItem(Items newItem)
 {
     itemInDaSlot = newItem;

     icon.sprite = itemInDaSlot.icon;
     icon.enabled = true;
 }
 public void ClearSlot()
 {
     itemInDaSlot = null;

     icon.sprite = null;
     icon.enabled = false;
 }


 public void Update()
 {
     if (SlotYellow.activeSelf == true)
     { thisSlotIsActive = true; }
     else { thisSlotIsActive = false; }

     //automatically equip an item if this slot is active
     if (thisSlotIsActive == true && itemInDaSlot!=null) { equipManager.Equip(itemInDaSlot); }
     else if (thisSlotIsActive == false && itemInDaSlot!=null) { equipManager.Unequip(); }
 }

}

public class ItemPickup : Interactables { public Items item;

 public override void Interact()
 {
     base.Interact();
         PickUp();
 }

 void PickUp()
 {
     Debug.Log("Picking up item ="+ item.name);


     bool wasPickedUp = Inventory.instance.Add(item,0);

//I input the number for the first slot(number 0) because I was going to make a code in the Add function to check if the slot is occupied, if it is, the number will goes up by 1 to see the next slot space to put the picked up item on, but i haven't succeed to come up with the code yet

     if (wasPickedUp)
     {
         gameObject.SetActive(false);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding a prefab to the Item class instance 0 Answers

How Can I make items inventory? (HELP) 0 Answers

IPointerEnterHandler and IPointerExitHandler not working 1 Answer

Inventory Master 0 Answers

how to use 3d objects for inventory slots 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges