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 /
avatar image
0
Question by iKamoficial · Dec 11, 2019 at 06:50 PM · 2d gameinventoryinventory systemstacking

Stack Problem(Please help!)

I have an inventory script on the player an an object with the itemdatabase. I create a label on each item with the stackamount of the item. I store the amount in each item and update it for each of them but when I have two items with the same id instead of creating a new one with 1 stackAmount it creates one that has an amount linked to the first item. Like if I change the stackAmount it changes for both of them.

InventoryScript:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Inventory : MonoBehaviour
 {
     public int slotsX, slotsY;
     public GUISkin skin;
     public List<Item> inventory = new List<Item>();
     public List<Item> slots = new List<Item>();
     public bool showInventory;
     private ItemDatabase database;
     private bool showTooltip;
     private string tooltip;
 
     public bool usingItem;
     int lastBlockUsed;
 
     private bool draggingItem;
     private Item draggedItem;
     private int previousIndex;
 
     float screenRatio;
     Vector2 ScaleOnRatio = new Vector2(0.1f, 0.1f);
     GameObject player;
 
 
     void Start()
     {
         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
         player = GameObject.FindGameObjectWithTag("Player");
 
         for (int i = 0; i < (slotsX * slotsY); i++) 
         {
             slots.Add(new Item());
             inventory.Add(new Item());
         }
 
         AddItem(0);
         AddItem(0);
         AddItem(0);
         AddItem(0);
         AddItem(1);
         AddItem(1);
     }
 
     void Update()
     {
         screenRatio = Screen.width / Screen.height;
                
         if (Input.GetKeyDown(KeyCode.E))
         {
             showInventory = !showInventory;
         }
         if (player.GetComponent<Player>().BlockPlaced == true)
         {
             showInventory = true;
             player.GetComponent<Player>().BlockPlaced = false;
         }
     }
 
     void OnGUI()
     {
         tooltip = "";
         GUI.skin = skin;
 
         if (usingItem && Input.GetKeyDown(KeyCode.E))
         {
             AddItem(lastBlockUsed);
             GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().PrefabBlock = null;
             usingItem = false;
         }
 
         if (showInventory)
         {
             DrawInventory();
         }
 
         if (showTooltip && showInventory)
             GUI.Box(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
 
         if (draggingItem)
             GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
         
     }
 
     void DrawInventory()
     {
         Event e = Event.current;
 
         int i = 0;
         for (int y = 0; y < slotsX; y++)
         {
             for (int x = 0; x < slotsY; x++)
             {
                 Rect slotRect = new Rect(x * 70, y * 70, 700 * ScaleOnRatio.x, 700 * (screenRatio * ScaleOnRatio.y));
 
                 GUI.Box(new Rect(slotRect), "", skin.GetStyle("Slot"));
                 
                 slots[i] = inventory[i];
 
                 if (slots[i].itemName != null)
                 {
                     GUI.DrawTexture(slotRect, slots[i].itemIcon);
 
                     if(slots[i].itemAmount > 1)
                     GUI.Label(slotRect, "" + slots[i].itemAmount, skin.GetStyle("Stack"));
 
                     if (slotRect.Contains(e.mousePosition))
                     {
                         if (!draggingItem)
                         {
                             CreateTooltip(slots[i]);
                             showTooltip = true;
                         }
 
                         if (e.button == 0 && e.type == EventType.MouseDrag && !draggingItem)
                         {
                             draggingItem = true;
                             previousIndex = i;
                             draggedItem = slots[i];
                             inventory[i] = new Item();
                         }
                         if (e.type == EventType.MouseUp && draggingItem)
                         {
                             inventory[previousIndex] = inventory[i];
                             inventory[i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
 
                         if (e.isMouse && e.type == EventType.MouseDown && e.button == 1)
                         {
                             if (slots[i].itemType == Item.ItemType.Block || slots[i].itemType == Item.ItemType.Trap)
                             {
                                 UseItem(slots[i], i, true);
                             }
                         }
                     }
                 }
                 else
                 {
                     if (slotRect.Contains(e.mousePosition))
                     {
                         if(e.type == EventType.MouseUp && draggingItem)
                         {
                             inventory[previousIndex] = inventory[i];
                             inventory[i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
                     }
                 }
 
                 if (tooltip == "")
                 {
                     showTooltip = false;
                 }
                 i++;
             }
         }
     }
 
     string CreateTooltip(Item item)
     {
         if (item.itemType == Item.ItemType.Trap)
             tooltip = "<color=#b85035>" + item.itemName + "</color>";
         else
         tooltip = item.itemName;
 
         return tooltip;
     }
 
     void UseItem(Item item, int slot, bool deleteItem)
     {
         usingItem = true;
         lastBlockUsed = item.itemID;
 
         GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().PrefabBlock = Resources.Load<GameObject>("PrefabBlock/" + item.itemName);
 
         if (deleteItem && item.itemAmount <= 1)
         {
             inventory[slot] = new Item();
         }
         else if (deleteItem && item.itemAmount > 1)
         {
             item.itemAmount -= 1;
         }
 
         if (player.GetComponent<Player>().BuildMode == true && player.GetComponent<Player>().BlockPlaced == false)
             showInventory = false;
 
 
         showTooltip = false;
     }
 
     void RemoveItem(int id)
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemID == id)
             {
                 inventory[i] = new Item();
                 break;
             }
         }
     }
 
     public void AddItem(int id)
     {
 
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i].itemAmount == inventory[i].stackMax || inventory[i].itemName == null)
             {
                 for (int j = 0; j < database.items.Count; j++)
                 {
                     if (database.items[j].itemID == id)
                     {
                         inventory[i] = database.items[j];
                     }
                 }
                 break;
             }
 
             if (ItemInInventory(inventory[i], id) && inventory[i].itemAmount < inventory[i].stackMax)
             {
                 if (inventory[i].itemID == id)
                 {
                     slots[i].itemAmount += 1;
                     break;
                 }
             }
         }
     }
 
     bool InventoryContains(int id)
     {
         bool result = false;
         for (int i = 0; i < inventory.Count; i++) 
         {
             result = inventory[i].itemID == id;
             if (result)
             {
                 break;
             }
         }
         return result;
     }
 
     bool ItemInInventory(Item item, int id)
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i].itemID == item.itemID)
                 return true;
         }
         return false;
     }
 }
 
 
 
 

Item Database:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemDatabase : MonoBehaviour
 {
     public List<Item> items = new List<Item>();
 
     void Start()
     {
         items.Add(new Item("SpikeTrap", 0, "A spike that appears from the ground ", 1, 0, Item.ItemType.Trap, 16));
         items.Add(new Item("WoodFloor", 1, "a ", 1, 0, Item.ItemType.Block, 16));
         items.Add(new Item("WoodFloor", 1, "a ", 1, 0, Item.ItemType.Block, 16));
     }
 }

ItemCreateScript:

 using System.Collections;
 using UnityEngine;
 
 [System.Serializable]
 public class Item
 {
     public string itemName;
     public int itemID;
     public string itemDesc;
     public Texture2D itemIcon;
     public int itemAmount;
     public int itemSpeed;
     public ItemType itemType;
     public int stackMax;
 
 
     public enum ItemType
     {
         Weapon,
         Trap,
         Monster,
         Block
     }
 
     public Item(string name, int id, string desc, int amount, int speed, ItemType type, int stackmax)
     {
         itemName = name;
         itemID = id;
         itemDesc = desc;
         itemIcon = Resources.Load<Texture2D>("Icons/" + name + "Icon");
         itemAmount = amount;
         itemSpeed = speed;
         itemType = type;
         stackMax = stackmax;
 
     }
     public Item()
     {
         itemID = -1;
     }
 }
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

150 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

Related Questions

Need help with my 2D inventory slot system 1 Answer

Searching your inventory for an item,How do i search for an item in my inventory? 1 Answer

problem with item stacking 0 Answers

Inventory Drop Function Problem 1 Answer

What is a good component to use for a GUI inventory display? 1 Answer


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