• 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 /
This question was closed Nov 03, 2018 at 05:03 PM by Gecuit for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Gecuit · Nov 02, 2018 at 07:48 PM · listinventoryinstantiationitemitem pickup

[SOLVED] Inventory Stacking Problem [C#]

My goal is to create inventory based on the following rules: 1. When you pick up an item, check if it is already in our inventory 2. If it's not, instantiate the item in the next slot in inventory and add it to the list 3. If it's already added, increase the amount of the existing item. Here's the code:

     Item itemObj;
     bool goodToPickUp = true;
     int n = 0;
 
     public void AddItem(Item item)
     {
         if (itemList.Contains(item)) // Check if we already have the item
         {
             goodToPickUp = false;
         }
         else
         {
             goodToPickUp = true;
         }
 
         // Adds an item if we don't have it in our inventory yet
         if (goodToPickUp)
         {
             itemObj = Instantiate(item, slots[n].transform, false); // Instantiate the item and store it in itemObj variable
             n++;
             itemList.Add(item);
         }
         else
         {
             for (int i = 0; i < itemList.Count; i++)
             {
                 if (itemList[i].Equals(item))
                 {
                     itemObj.amount++;
                     // itemObj doesn't change, so no matter what item will be picked up now, amount of the last will be increased
                 }
             }
         }
     }

The problem: itemObj doesn't update if the item has already been in our inventory. As a result, only the amount of the last item picked up is increased. I've tried to put in in words as well as I could, please ask questions if something is unclear.


EDIT:

Here's a video that might shed some light on what I'm trying to fix

Comment
Add comment · Show 1
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
avatar image hexagonius · Nov 02, 2018 at 09:13 PM 0
Share

you're only assigning itemObj a value when you instantiate something. is you want to use it in the else case, assign it the value you found.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Gecuit · Nov 03, 2018 at 02:40 PM

Hah, I just managed to fix it. My solution was to create a separate list for instances and separate for prefabs.

 public List<Item> itemList = new List<Item>(); // List storing of all objects picked up
 public List<Item> itemObjList = new List<Item>(); // List storing instances of all objects picked up

Then I changed if statement in AddItem(Item) to this:

         // Adds an item if we don't have it in our inventory yet
         if (goodToPickUp)
         {
             itemList.Add(item);
             itemObjList.Add(Instantiate(item, slots[n].transform, false)); // Instantiate the item and add it to the list
             n++;
         }
         else
         {
             for (int i = 0; i < itemList.Count; i++)
             {
                 if (itemList[i].Equals(item))
                 {
                     itemObjList[i].amount++; // Increases amount of the instance
                 }
             }
         }

@EgoAnt your answer kind of guided me what to do, but it's not correct itself. Thank you for your help anyway :)

Comment
Add comment · Show 1 · Share
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
avatar image AaronXRDev · Nov 03, 2018 at 03:05 PM 1
Share

Awesome! I was just doing a quick code sample to show exactly this. Glad you solved it!

avatar image
1

Answer by AaronXRDev · Nov 02, 2018 at 10:29 PM

Looks like you just need to change:

 itemObj.amount++;

to:

 itemList[i].amount++;


If that doesn't work, do you mind posting the class code for Item here?

Comment
Add comment · Show 1 · Share
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
avatar image Gecuit · Nov 03, 2018 at 09:17 AM 0
Share

Unfortunately, that didn't solve the problem, because itemList contains prefabs, and not instances. Therefore it increases the amount of prefab, without doing anything in the scene. And here's Item class which you asked for:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Item : $$anonymous$$onoBehaviour
 {
     public int amount = 1;
     public string itemName;
 
     // Update UI
     void Update()
     {
         transform.GetChild(0).GetComponent<UnityEngine.UI.Text>().text = amount + "";
     }
 }

The first solution that comes to my mind would be to change itemList.Add(item); line to itemList.Add(itemObj); in the AddItem(Item) method. However then, if (itemList.Contains(item)) will always return false, because the list will be made up of instances and not prefabs.

And if I change the if statement to check for itemObj ins$$anonymous$$d of item, it won't work because itemObj hasn't yet been assigned.

For clarification itemObj is an instance of item. I start to think that I should rewrite the entire inventory system because this one got too complicated.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

100 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

Related Questions

How i can make a script-made button interactable? 0 Answers

Scroll List Problem 1 Answer

Preventing Items from shifting in a list 1 Answer

DontDestroyOnLoad make errors 0 Answers

How to add item to my inventory when I pick them up? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges