Need Help with IDragHandler -- The method or operation is not implemented.

So I have been trying to make a draggable object for my inventory, and here is what I have so far

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    private RectTransform rectTransform;

    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
    }

    public void OnPointerDown(PointerEventData eventData)//This funciton (plus IPointerDownHandler in the class section) detects when the mouse clicks on the gameObject this is attached to
    {
        throw new System.NotImplementedException();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }

    public void OnDrag(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
        rectTransform.anchoredPosition += eventData.delta; //Moves the item as it is being dragged (delta is the amount that the mouse moved since the previous frame)
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }



    void Start()
    {

    }

    void Update()
    {

    }
}

However when I try to play it, it keeps erroring out –
NotImplementedException: The method or operation is not implemented.
DragDrop.OnDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/DragDrop.cs:27) –

I have tried looking into it, and I have added the Hierarchy EventSystem, but I haven’t messed with it. I also have an image component on my object as that is apparently required. Also on my Canvas I have a Graphic Raycaster component if that is needed.

So what am I doing wrong/need yet?
Thanks!

You see all the lines of code you have that say this?

throw new System.NotImplementedException();

Those lines are creating the error you see in the console. Your code is directly, explicitly generating that error. To get rid of the error, remove those lines of code. Visual Studio put those there to remind you that you need to fill in those functions with real code. So if you have filled them in, it is time to remove that line.

3 Likes

Mental FacePalm xD. Dang it, why does it have to be that easy xD

Note that the reason why Visual Studio adds those “throw” lines instead of just implementing the methods “empty” is because potentially methods that have a return type need to have a return statement with a value return value. Since VS can not determine what would be a “sensible” return value, it simply adds this line. When an exception is thrown, the method is immediately terminated and the enclosing error handler will be called. If the call is not placed in a try / catch manually, it will be Unity that is catching the error.

Also when you implement an interface, it helps to not forget to implement the functionality of a method. Always keep in mind that errors are your friend. They tell you about potential issues so you can fix them. In this case you just want to remove the explicitly thrown exception once you have implemented the actual functionality.

1 Like