Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Image Event Trigger BeginDrag and EndDrag not working

Discussion in 'Scripting' started by ArilynArt, Apr 12, 2020.

  1. ArilynArt

    ArilynArt

    Joined:
    Apr 12, 2020
    Posts:
    4
    Hey all, I'm new to C# and programming in unity and I'm making a card game to prototype something I've been working on. I was following a tutorial for Drag and Drop in Unity 2D that I was attempting to apply to my image but for some reason the Event Trigger component I put on the image isn't triggering the selected function on the script that's on the same Image Object.

    I've attempted this with different Image Objects (even brand new ones) and still nothing.

    This has got me stumped on why it's not working. I have 0 errors and everything seems to work just fine but the print I called in the functions aren't showing either.Thanks in advance!

    Here's the code for the drag and drop script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DragDrop : MonoBehaviour
    7. {
    8.     private bool isDragging = false;
    9.  
    10.     private Vector2 startPosition;
    11.     void Update()
    12.     {
    13.         if (isDragging)
    14.         {
    15.             transform.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    16.         }
    17.     }
    18.  
    19.     public void StartDrag()
    20.     {
    21.         startPosition = transform.position;
    22.         isDragging = true;
    23.         print("startdrag");
    24.     }
    25.  
    26.     public void EndDrag()
    27.     {
    28.         isDragging = false;
    29.         print("enddrag");
    30.     }
    31. }
    Here's the tutorial:
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Is there an EventSystem component in your scene hierarchy somewhere?
     
  3. ArilynArt

    ArilynArt

    Joined:
    Apr 12, 2020
    Posts:
    4
    I figured it out. The Canvas I was using to detect the input was a World View and needed an Event Camera. Sorry, thanks!!
     
  4. JanetGilbertPraxis

    JanetGilbertPraxis

    Joined:
    May 6, 2022
    Posts:
    21
    Me too. Accidentally turned off the event system then wondered why it wasn't working!