Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by NotThatSpecial · Apr 21, 2015 at 02:25 PM · inventory

Need help with Inventory stacker. PLEASE. Almost working!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 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>();
     private bool showInventory;
     private ItemDatabase database;
     private bool showTooltip;
     private string tooltip;
     
     public GameObject player, mainCamera;
     public bool mouseLook;
     
     public bool draggingItem;
     public Item draggedItem;
     public int prevIndex;
     
     public int box;
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         mainCamera = GameObject.FindGameObjectWithTag("CameraHead");
         mouseLook = true;
         
         for(int i = 0; i < (slotsX * slotsY); i++)
         {
             slots.Add(new Item());
             inventory.Add(new Item());
         }
         
         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
 
 
     }
     
     void Update()
     {
         Screen.showCursor = true;
         if(mouseLook)
         {
         //    player.GetComponent<MouseLook>().enabled = true;
         //    mainCamera.GetComponent<MouseLook>().enabled = true;
             
         }
         else
         {
             //player.GetComponent<MouseLook>().enabled = false;
         //    mainCamera.GetComponent<MouseLook>().enabled = false;
         }
         
         if(Input.GetKeyDown(KeyCode.I))
         {
             mouseLook = !mouseLook;
             showInventory = !showInventory;
             AddItem(0);
         }
     
 
     }
     
     void OnGUI()
     {
         if(Network.peerType != NetworkPeerType.Disconnected)
         {
             tooltip = "";
             
             GUI.skin = skin;
             if(showInventory)
             {
                 DrawInventory();
                 if(showTooltip)
                     GUI.Box(new Rect(Event.current.mousePosition.x + 15f,Event.current.mousePosition.y,200,200),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 < slotsY; y++)
         {
             for(int x = 0; x < slotsX; x++)
             {
                 Rect slotRect = new Rect(x * 60 + Screen.width/2 - slotsX * 30, y * 60 + Screen.height/2 - slotsY * 30,50, 50);
                 if(!slots[i].itemStackable)
                     GUI.Box (slotRect, "");
                 if(slots[i].itemStackable)
                     GUI.Box (slotRect, slots[i].itemStacked.ToString());
                 
                 slots[i] = inventory[i];
                 box = i;
                 if(slots[i].itemName != null)
                 {
                     GUI.DrawTexture(slotRect, slots[i].itemIcon);
                     if(slotRect.Contains (e.mousePosition))
                     {
                         CreateTooltip(slots[i]);
                         showTooltip = true;
                         if(!slots[i].itemStackable)
                         {
                             if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
                             {
                                 draggingItem = true;
                                 prevIndex = i;
                                 draggedItem = slots[i];
                                 inventory[i] = new Item();
                             }
                             if(e.type == EventType.mouseUp && draggingItem)
                             {
                                     inventory[prevIndex] = 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.Consumable)
                                 {
                                     UseConsumable(slots[i].itemID,i,true);
                                     print ("USED CONSUMABLE: " + slots[i].itemName + "," + slots[i].itemID);
                                 }
                             }
                         }
                         if(slots[i].itemStackable)
                         {
                             if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
                             {
                                 draggingItem = true;
                                 prevIndex = i;
                                 draggedItem = slots[i];
                                 inventory[i] = new Item();
                             }
                             if(e.type == EventType.mouseUp && draggingItem)
                             {
                                 if(slots[i].itemID != draggedItem.itemID)
                                 {
                                     draggingItem = false;
                                     inventory[prevIndex] = inventory[i];
                                     inventory[i] = draggedItem;
                                     draggedItem = new Item();
                                 }
                                 if(slots[i].itemID == draggedItem.itemID)
                                 {
                                     draggingItem = false;
                                     inventory[i].itemStacked += draggedItem.itemStacked;
                                     draggedItem = new Item();
 
                                 }
                             }
 
 
                             if(e.isMouse && e.type == EventType.MouseDown && e.button == 1)
                             {
                                 if(slots[i].itemType == Item.ItemType.Consumable)
                                 {
                                     UseConsumable(slots[i].itemID,i,true);
                                     print ("USED CONSUMABLE: " + slots[i].itemName + "," + slots[i].itemID);
                                 }
                             }
                         }
                     }
 
 
                     
                 }
                 else
                 {
                     if(slotRect.Contains (e.mousePosition))
                     {
                         if(e.type == EventType.mouseUp && draggingItem)
                         {
                             Debug.Log("Placed " + draggedItem.itemName + " at " + i);
                             inventory[i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
                     }
                 }
                 
                 
                 if(tooltip == "")
                 {
                     showTooltip = false;
                 }
                 
                 
                 i++;
 
             }
         }
     }
 
     
     private void UseConsumable(int id, int slot, bool deleteitem)
     {
         if(id == 2)
         {
             //increase stat
         }
         if(deleteitem)
         {
             if(inventory[slot].itemStacked > 1)
             {
                 inventory[slot].itemStacked --;
                 
             }
             else
             {
                 inventory[slot] = new Item();
             }
         }
     }
     
     string CreateTooltip(Item item)
     {
         tooltip = item.itemName + "\n" + item.itemDesc + "\n" + item.itemPower;
         return tooltip;
     }
     
     void RemoveItem(int id)
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemID == id)
             {
                 inventory[i] = new Item();
                 break;
             }
         }
     }
     
     void AddItem(int id)
     {
         for(int i = 0; i<inventory.Count; i++)
         {
             if(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;
             }
         }
     }
     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;
     }
     
 }




Whenever I try to stack the items that are the same items and have multiple of them in my inventory, it just multiplies the amounts. i tried adding the dragging items amount to the slots amount but that doesn't work and then the other items in the inventory show the same amount of items stacked. PLEASE HELP

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 matmel · Jun 11, 2015 at 04:06 PM 0
Share

Hey there, can i take a look at your Item script and ItemDatabase script?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Confirming item pickup in inventory 0 Answers

How to get an item that's in my inventory to instantiate into the players hand when i click on the item in my inventory. 2 Answers

Using enums to design an inventory system? 1 Answer

Issues with Inventory script 0 Answers

Object being destroyed without GetKey Down 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