I can't get OnMouseDrag to work With GUI elements.

for some reason i can’t get on mouse drag to work with GUI elements.
i have a dimple dragg script

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class DraggScript : MonoBehaviour {

	// userInputSimple user;

	[SerializeField] Vector2 startPoint;
	[SerializeField] Vector2 endPoint;
	[SerializeField] bool drag;


	void OnMouseDrag() 
	{
		drag = true;
		// user.dragging(false);
		endPoint = Input.mousePosition;


		// rend.material.color -= Color.white * Time.deltaTime;
	}
	void OnMouseUp() {
		user.dragging(true);
		drag =false;
	}
	void OnMouseDown() {
		if(!drag){
			startPoint = Input.mousePosition;
		}
	}
}

i should be able drop it on any guielement and it should work but onMouseDragg is never called
on gui elements.

if you right-click in the scene hierarchy create new panel and drag this script onto the panel it should work?

-by default it wont be in the ignore raycast layer it is in the UI layer.
-panels are gui elements i tried with buttons still did not work.

if i toss it on a plane then attach it to the camera it works
but for some reason it does not work for GUIelements

even though in the documentation clearly states “This event is sent to all scripts attached to the Collider or GUIElement.”

i am Missing something i dont know what it is.

You should use IDragHandler, IBeginDragHandler and IEndDragHandler interfaces for GUI. I tried to rewrite your code, it should work:

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

public class DraggScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    [SerializeField] 
    Vector2 startPoint;
    
    [SerializeField]
    Vector2 endPoint;

    [SerializeField] 
    bool drag;

    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        startPoint = eventData.pressPosition;
    }

    public void OnDrag(PointerEventData eventData)
    {
        drag = true;
        endPoint = eventData.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        drag = false;
    }
}

Yes, you are right, OnMouseDow and Up are called for GUIElements adn colliders, but not UnityEngine.UI elements… u have to use the EventSystem and EventTrigger Drag stuff to work with a drag inside a Canvas game object(or in other words UI game object).