Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question about OnPointerUp

Discussion in 'Getting Started' started by killerKeks26601, Mar 17, 2021.

  1. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    Hi,
    I have a question about the OnPointerUp event.
    I am making an inventory system and im trying to make the objects drag and dropable in the itemslots.
    I have 1 on the item slot aswell as on the selected item object.
    First i used the OnPointerClick event which worked perfect.
    But i decided i rather want to use OnPointerDown and OnPointerUp so that it drags and drops with one mouse click and one release, but now the item gets snapped back to the spot where i picked it up instead of where i released the mouse button.
    I wouldve thought that OnPointerUp gets called where you actually release the button not where you first pressed it down.
    Is it supposed to work like this and I have to use the drag events or am i missing something else.
    Here my script for reference:
    Code (CSharp):
    1.     public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         if(this.item != null)
    4.         {
    5.             if(selectedItem.item != null)
    6.             {
    7.                 Item clone = new Item(selectedItem.item);
    8.                 selectedItem.UpdateItem(this.item);
    9.                 UpdateItem(clone);
    10.             }
    11.             else
    12.             {
    13.                 selectedItem.UpdateItem(this.item);
    14.                 UpdateItem(null);
    15.             }
    16.         }
    17.     }
    18.     public void OnPointerUp(PointerEventData eventData)
    19.     {
    20.         if(selectedItem.item != null)
    21.         {
    22.             UpdateItem(selectedItem.item);
    23.             selectedItem.UpdateItem(null);
    24.         }
    25.     }
     
  2. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    PS : The update funtions to change the images on the slot aswell as on the selected item
    Code (CSharp):
    1. public void UpdateItem(Item item)
    2.     {
    3.         this.item = item;
    4.         if(this.item != null)
    5.         {
    6.             spriteImage.color = Color.white;
    7.             spriteImage.sprite = this.item.icon;
    8.         }
    9.         else
    10.         {
    11.             spriteImage.color = Color.clear;
    12.         }
    13.     }
    Also the item follows the cursor just fine with another script where i just set the position to the cursor in the update function
     
    Last edited: Mar 17, 2021