• 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.

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

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 System ? 0 Answers

Argument out of range? 2 Answers

Inventory Script 0 Answers

Canvas Inventory closing 0 Answers

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