scrollRect.OnDrag when the script is not in that object

Hello all,

I think I’m going as something very basic but as a newbie, I do not know the answer (but I search for it!):

I have an script in a empty object.

It has some public variables that I assigned to objects, one is “scrollView” which is a scrollRect.

Everything works very well but I would like to know how detect when the user is dragging scrollView.

I know that it needs to be something related to onDrag but, all the examples I found works with the script attached to that object.

Can I do it from my script?

Thanks a lot.

Best regards,

Juanjo

You can attach IDragHandler interface within UnityEngine.EventSystems to detect drag event.

using UnityEngine;
using UnityEngine.EventSystems;

public class DragHandler : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        //...
    }
}

https://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html

Thanks for your quick reply but I think I’m too newbie to understand…

my script is named mainMenu there I have my variable:

public scrollRect scrollView; // (linked with the editor)

can I do something like scrollView.onDrag?

I’ve tried to have mainMenu : Monobehaviour, IDragHandler… but it gives me error XD

I feel asking something very basic XD

It gives you error probably because you are missing namespace UnityEngine.EventSystems or OnDrag function.

You can override ScrollRect component and replace it with existing one, but that’s a different approach.

//ScrollRectExtended.cs
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ScrollRectExtended : ScrollRect
{
    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        //...
    }
}

I would recommend you to use EventTrigger component. Just place it on same gameObject as ScrollRect and call your function from mainMenu. Once you understand how EventTrigger works (Redirecting to latest version of com.unity.ugui) you can switch to using interfaces instead.

1 Like

Mmmm I think the EventTrigger sounds like a good solution :slight_smile: But I need to learn how to use it from my script.

I’ve tried the other solution you gave me and the error was:

mainMenu' does not implement interface member UnityEngine.EventSystems.IDragHandler.OnDrag(UnityEngine.EventSystems.PointerEventData)’

Probably because I@m coming from Corona and Lua but this sounds like Chinese to me XD

My current script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class mainMenu : MonoBehaviour {

    public ScrollRect scrollView;

void Start()
{
}

}

Where and how should I do the Trigger? or the onDrag? or both (just with learning purposes :)).

And… thanks a lot for your patience! Much appreciated!

It’s an component.

Attach it next to ScrollRect and add drag event type.

and… capitalize your class names!
mainMenu > MainMenu

2 Likes

Hello sir,

I tested and it works! Like a charm!

Super thanks!

And I also renamed my class :slight_smile: Always learning new things!!!

Super thanks again!