- Home /
OnDrag, OnPointerEnter, OnMouseOver for gameobjects on a canvas
I have a canvas with 4 'slots' on it. These slots are just gameobjects.
I'm looking to be able to drag these slots. So I have one script attached to the canvas with the 4 slots created in that script and given names.
I want to be able to click and hold on one of the slots and have the system know that I have chosen to drag 'Slot 3' for example. At the moment the most accurate I can get is literally just being able to detect that I am dragging the canvas as a whole, but I want to be able to drag the slot gameobjects on this canvas.....
I have tried several ways so far...
public void OnDrag(PointerEventData data)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.collider.name);
}
}
Using a combination of OnDrag and raycast it doesn't detect the gameobject slots on the canvas, the only output of the above code is ..."Terrain"...so it just skips the canvas and points at the terrain behind it...No luck here
My second attempt is below....
public void OnPointerEnter(PointerEventData data)
{
print(gameObject.name);
}
Output: "equipfield"...this is the name of the canvas...
So the most luck I have had is with OnPointerEnter but still can't get it to detect the individual slot gameobjects.
Anyone have any ideas how I can do this?