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 KyleStank · Sep 09, 2016 at 10:37 AM · c#uiinventoryinventory systemdrag and drop

Why is drag and drop not working?

I am making a drag and drop inventory system, and so far, it's going pretty well. But I have ran into a crazy and annoying issue. When an item is picked up, it is displayed in the inventory. The item can be dragged to the 2nd, 3rd, or 4th slot of the 4 slot inventory, but if I try to drag it back to the 1st inventory slot, it does not work. Here is the code: [code]void IEndDragHandler.OnEndDrag(PointerEventData eventData) { if(DisplayItem == null) return;

 //Set Slot back to original position
 IconImage.rectTransform.position = originalPos;
 IconImage.transform.parent.SetSiblingIndex(previousIndex);

 Transform parent = IconImage.transform.parent.parent;
 Slot[] slots = new Slot[parent.childCount];

 for(int i = 0; i < slots.Length; i++) {
     Transform slotTransform = parent.GetChild(i);
       
     Slot slot = slotTransform.GetComponent<Slot>();
       
     float width = slot.IconImage.rectTransform.rect.width;
     float height = slot.IconImage.rectTransform.rect.height;

     Vector2 slotPos = slot.transform.position;

     float distance = Vector2.Distance(eventData.position, slotPos);
     print("NAME:" + slot.name + " - " + distance);
     if(distance < 100.0f) {
         transform.SetSiblingIndex(i);
         IconImage.rectTransform.position = slot.IconImage.rectTransform.position;
     }
 }

}[/code] I know the problem is that when the Item isn't in the 1st slot, on the line where I am printing out the distance, the same exact distance is printed twice, yet with different game object names. So please help me figure out why I can't drag back to the first slot!

Thanks, Kyle

EDIT: I have just found out that if there is 2 items in the inventory, then items can only be dragged to the 3rd and 4th slots, and not the 1st and 2nd slots. And if there are 3 items then only slot 4... So on and so on. I really have no idea what is happening anymore. If more code is needed, please, let me know!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KyleStank · Sep 10, 2016 at 05:06 PM

Omfg I got. Such a facepalm moment. On line 26, I put IconImage.rectTransform.position= slot.IconImage.rectTransform.position;

This should be IconImage.rectTransform.position = slot.transform.position; I could also use the slotPos variables instead. This is because the current slot's original position of it's icon may have changed, so using it's icon position is not what we want. Omg, I can't believe I made this mistake and didn't catch it for 3 days LOL.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Display list as UI or GUI 0 Answers

Inventory Script 0 Answers

switch between 5 Ui icons by pressing a button 0 Answers

Canvas Inventory closing 0 Answers

How to make an Inventory Hotbar (C#) 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