Problem with getting objects position

Hi.
So, I have few UI elements with a drag and drop script (first code below, named DragnDropCards).
And I also have few others UI elements with a slot script (second code below, named RoleSlot).

These two scripts work together so when I drop a element with the drag and drop script, it snaps to the middle of the UI element with the slot script.

I followed a youtube tutorial and it worked just fine (

).
The thing is, I’d like to change the tag of the element with the drag and drop script once it gets into the position of the elements with the slot script.

My idea is to make like a card game, when I place a card (the elements with drag and drop script) into a slot (with the slot script) it gets a tag so i know that its being used. If its not into that slot position, I wish it would get untagged.

What I tried to do to solve this is, when the OnEndDrag is activated, create a if statement that would say “if the position of this object is the same position of one of the objects with the slot script attached to it, tag it as FirstLine, else tag = untagged”. But I found a hard time when trying to make this statement.

If anyone could help, I’d be really thankfull. Or even if someone could provide me with something to study to understand it better.

Thanks!

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

public class DragnDropCards : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    [SerializeField] private Canvas canvas;

    private RectTransform rectTransform;
    private CanvasGroup canvasGroup;
    public GameObject[] roleSlots;

    private void    Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();
        roleSlots = GameObject.FindGameObjectsWithTag("Slot");
    }

    public  void    OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("Arrastou");
        canvasGroup.blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("NoArrasto");
        rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("Parou");
        canvasGroup.blocksRaycasts = true;

    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Clicou");
    }


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

public class RoleSlot : MonoBehaviour, IDropHandler
{


    public void OnDrop(PointerEventData eventData)
    {    
        Debug.Log("Dropou");
        if (eventData.pointerDrag != null)
        {
            eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
        }
    }
}

I don’t see any position comparison code above.

To compare positions, you want to NOT use equality because of floating point precision issues.

Rather calculate their distance, and choose to act when it is small enough for you consider them equal:

if ( Vector3.Distance( card.transform.position, slot.transform.position) < 0.1f)
{
  // yes, it is close enough, do stuff
}

Remember that will compare the little blue dot in the UI editor only. If you want bounds checking use the RectTransform component’s .rect property and check that for being included. There’s tons of examples of rectangle checking, and the rect structure actually has a .Contains() method and .Overlaps() method you can use.