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 /
  • Help Room /
avatar image
0
Question by TurboPipo · Jun 28, 2020 at 07:49 AM · inventory systemremoveitemdrop

Inventory script help and how to remove items from inventory

Hi guys,

I found this inventory script on GitHub and for the most part, it's been working fine and saving me from having to code a script myself: https://github.com/nzhul/inventory-system

There are some parts of the script I don't understand, such as the else if statement in the below excerpt:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using System;
 
 public class Slot : MonoBehaviour, IDropHandler
 {
     public int id;
     private Inventory inv;
     public RectTransform invPanel;
 
     void Start()
     {
         inv = GameObject.Find("Inventory").GetComponent<Inventory>();
 
     }
 
     public void OnDrop(PointerEventData eventData)
     {
         ItemData droppedItem = eventData.pointerDrag.GetComponent<ItemData>();
 
         if (inv.items[id].Id == -1)
         {
             inv.items[droppedItem.slotId] = new Item();
             inv.items[id] = droppedItem.item;
             droppedItem.slotId = id;
             Debug.Log("Testing 1");
         }
 
         // ???
         else if (droppedItem.slotId != id)
         {
             Transform item = this.transform.GetChild(0);
             item.GetComponent<ItemData>().slotId = droppedItem.slotId;
             item.transform.SetParent(inv.slots[droppedItem.slotId].transform);
             item.transform.position = inv.slots[droppedItem.slotId].transform.position;
 
             droppedItem.slotId = id;
             droppedItem.transform.SetParent(this.transform);
             droppedItem.transform.position = this.transform.position;
 
             inv.items[droppedItem.slotId] = item.GetComponent<ItemData>().item;
             inv.items[id] = droppedItem.item;
             Debug.Log("Testing 2");
         }
 
     }
 }

Apart from this, I have been trying to build a way to remove items from the inventory, which doesn't seem to be part of the script. I tried adding the below snippet into the OnDrop() function:

 else if(RectTransformUtility.RectangleContainsScreenPoint(invPanel, Input.mousePosition))
         {
             Debug.Log("Item dropped");
         }

And this into my Start() function:

         invPanel = GameObject.Find("InventoryPanel").GetComponent<RectTransform>() as RectTransform;

But the code doesn't do anything. Any advice on what to do? Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TurboPipo · Jun 28, 2020 at 12:31 PM

Okay, that portion of code in else if() lets you change positions of one item with another item.. but I still haven't figured out how to drag/drop and remove an item from the inventory. Any help would be greatly appreciated. Thanks!

Comment
Add comment · 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
0

Answer by streeetwalker · Jun 29, 2020 at 05:24 AM

@TurboPipo, You can't do it in the Slot class, by that time it is already too late - Because in the slot code you are dropping on a slot, it has no facility for knowing if you are dropping outside of a slot.

You have to implement the drag outside functionality at the higher level - probably you have a "container" class creates each slot, it is there you could check if you are dragging outside the bounds of the inventory object, or in some object you put in the background.

Where ever it goes, you put an OnDrop function and send a message (or call a method) to the item's current Slot that the item is being removed, and likewise I would guess to the inventory manager system.

Comment
Add comment · 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

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

203 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do I make a item system with ScriptableObjects where different items can have durability? 1 Answer

Item dropping/instantiating isn't working 2 Answers

Drop rate / rarity 2 Answers

Item is missing in inventory slot after i pick up and delete the item. C# 0 Answers

inventory and item system where every item need its own use function 0 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