• 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 prefix · Jan 23, 2017 at 02:42 AM · uidrag-and-dropevent-handling

Drag and Drop

Below is a snapshot of whats happening and what im trying to accomplish. When i drag an item to the appropriate slot, it stays. However when I drag a different item on top of the one that's already there, I want it to go back to its original (sub)parent/start position. What's happening is that all objects are going to the same (sub) parent.

Im not sure where im going wrong in code. (beginner programmer)

So the question is, how do i get the "already" dragged item, to return to its original parent when i drag another item on top of it?

Thanks

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
 
 
     /// <summary>
     /// THIS HANDLES THE SELECTING AND DRAGGING OF THE OBJECT
     /// </summary>
 
 
     public static GameObject itemBeingDragged;
 
     Vector3 startPosition;
     Transform startParent;
 
     public static Transform originalStartParent;
 
     #region IBeginDragHandler implementation
 
     void Start()
     {
         originalStartParent = gameObject.transform.parent;
     }
 
     public void OnBeginDrag (PointerEventData eventData)
     {
         itemBeingDragged = gameObject;
         startPosition = transform.position;
         startParent = transform.parent;
         GetComponent<CanvasGroup> ().blocksRaycasts = false;
     }
 
     #endregion
 
 
 
 
     #region IDragHandler implementation
 
     public void OnDrag (PointerEventData eventData)
     {
         transform.position = Input.mousePosition;
     }
 
     #endregion
 
 
 
 
     #region IEndDragHandler implementation
 
     public void OnEndDrag (PointerEventData eventData)
     {
         itemBeingDragged = null;
         GetComponent<CanvasGroup> ().blocksRaycasts = true;
         if (transform.parent == startParent) 
         {
             transform.position = startPosition;
         }
     }
 
     #endregion
 
 
 
 }

 //------------------------------------------------------------------------------------------------------------------
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class Slot : MonoBehaviour, IDropHandler {
 
 
     /// <summary>
     // THIS HANDLES WHICH SLOTS CAN GRAB THE ITEM 
     /// 
 
 
     public GameObject item
     {
         get
         {
             if (transform.childCount > 0) 
             {
                 return transform.GetChild (0).gameObject;
 
             }
             return null;
         }
 
      }
 
 
     #region IDropHandler implementation
     public void OnDrop (PointerEventData eventData)
     {
         if (!item)
             DragHandler.itemBeingDragged.transform.SetParent (transform);
         
         else if (item)
             item.transform.SetParent (DragHandler.originalStartParent);
             DragHandler.itemBeingDragged.transform.SetParent (transform);
 
 
     }
     #endregion
 
 
 }
 
 

alt text

helpfile.jpg (123.9 kB)
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.

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

124 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

Related Questions

IPointerExitHandler while holding click 0 Answers

How would I racast at the "OnEndDrag" to see what I hit (I want to parent my button to the slot it hits). 0 Answers

IDragHandler Interfaces handling multiple UIs 1 Answer

Snap objects back to their original place 0 Answers

Layermask like property for OnPointerEnter/Exit? 0 Answers

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