Search Unity

OnEndDrag problematically creating duplicate dragged items

Discussion in 'Immediate Mode GUI (IMGUI)' started by Skraggl, May 6, 2021.

  1. Skraggl

    Skraggl

    Joined:
    Jan 30, 2020
    Posts:
    2
    EDIT: SOLVED!

    Hi, first time posting. Apologies if this is redundant thread or incorrectly placed.

    My InventoryObject code currently works, up to a point. Player can drag and drop item into inventory slot. If item is not dropped into inventory slot, item returns to original position within scene.

    However, if I quickly click and release the item while it is still in the scene, a duplicate item is created. Attached is a picture where I clicked on the item several times, creating a new item each time. Since this happens when I release the mouse, I think the problem must be during the OnEndDrag() method, but I can't find anything wrong with it. Here is the method:

    Code (CSharp):
    1. public void OnEndDrag(PointerEventData pointerEventData)
    2.     {
    3.         if (ItemIsInsideInventory == false)
    4.         {
    5.             //if not dropped into inventory, dragged object should return to original position in scene
    6.             if (transform.parent == dragCanvas.transform)
    7.             {
    8.                 transform.position = instantiatedPos;
    9.                 transform.SetParent(sceneCanvas.transform);
    10.                 transform.localScale = originalLocalScale;
    11.                 GetComponent<Image>().raycastTarget = true;
    12.             }
    13.             else //if successfully dropped into inventory:
    14.             {
    15.                 parentToReturnTo = this.transform.parent.transform; //parentToReturnTo is the parent transform
    16.                 transform.position = parentToReturnTo.position;
    17.                 transform.localScale = originalLocalScale;
    18.                 GetComponent<Image>().raycastTarget = true;
    19.                 ItemIsInsideInventory = true;
    20.             }
    21.         }
    22.  
    23.         else //if (ItemIsInsideInventory == true)
    24.         {
    25.             if (transform.parent == dragCanvas.transform)
    26.             {
    27.                 transform.SetParent(parentToReturnTo); //previously set as inventory slot transform during OnDrop
    28.                 transform.position = parentToReturnTo.position;
    29.                 transform.localScale = originalLocalScale;
    30.                 GetComponent<Image>().raycastTarget = true;
    31.             }
    32.             else // item successfully dropped into different inventory slot
    33.             {
    34.                 parentToReturnTo = this.transform.parent.transform;
    35.                 this.transform.position = parentToReturnTo.position;
    36.                 transform.localScale = originalLocalScale;
    37.                 GetComponent<Image>().raycastTarget = true;
    38.             }
    39.         }
    40.     }
    If item is in an inventory slot, I can quickly click and release the item and it doesn't create duplicates. The problem only appears to be when an item is hanging out in the scene.

    A quick run-down of how I'm handling drag: When an item is instantiated in the scene, it is made a child of SceneCanvas. Upon OnBeginDrag, the item is made a child of DragCanvas. Upon OnEndDrag, if item's parent is still DragCanvas (and not InventorySlot), then item is made a child of SceneCanvas again and it returns to its original position in scene.

    I'm a beginner programmer, so I apologize if the code seems strangely convoluted. I would greatly appreciate any suggestions on trouble-shooting this problem or obviating the problem by changing my overall approach to handling drag.
     

    Attached Files:

    Last edited: May 6, 2021
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    Have you tried putting some Debug.Log() entries in the code to get a better idea of what is firing and when?

    Also, this is UI, not IMGUI (for reference).
     
    Skraggl likes this.
  3. Skraggl

    Skraggl

    Joined:
    Jan 30, 2020
    Posts:
    2
    Thanks for the suggestion and clarification! Turns out, the problem wasn't the drag method at all. The object beneath the item (the item spawner) was still active and simply instantiating new items whenever I clicked it!