Search Unity

How to trigger OnDrag on a instantiated UI gameobject with only 1 touch/click input

Discussion in 'Scripting' started by RustyCrow, Aug 3, 2019.

  1. RustyCrow

    RustyCrow

    Joined:
    Feb 28, 2014
    Posts:
    43
    Hello everyone, i have been trying to make a potion dispenser but it just dosent feel good unless it all happens in 1 smooth motion.
    The problem is essentially when i click on the dispenser(UI image) it instantiates a potion(right where i click the image) using the IPointerDownHandler interface, and then i have to click on the spawned potion to start dragging it using its own IBeginDragHandler, IDragHandler, IEndDragHandler script.

    What i want to happen is just 1 click(and hold the click) and start drag the instantiated potion. Take a look at the script below it will give a better picture.

    Code (CSharp):
    1. public class PotionDispenser : MonoBehaviour, IPointerDownHandler/*, IBeginDragHandler, IDragHandler, IEndDragHandler*/
    2. {
    3.  
    4.     public void OnPointerDown(PointerEventData eventData)
    5.     {
    6.          SpawnPoition(); // returns potion GO
    7.          // Somehow Trigger Drag event on the potion so OnBegingDrag(), OnDrag() will be called
    8.     }
    9.  
    10.  
    11. }
    This might be impossible to do with the unity Eventsystem passing on the "focus" from the dispenser to the potion so to speak. If someone can atlaset confirm that this is not possible i would be grateful and il try to find an alternative.