drag drop a object into list objectes

hey all. I wanna drag-drop an object into a list of objects . for example I have a blue object and wanna drag and drop into the white piece. I wrote code like this. but a little bug is in cuz a lot of things should check-in “if” and sometimes my blue object going back to his first position (i think for a lot of conditions and checking a lot of distance, system going to else of foreach ). so… I think need a better solution .if u have any idea help mealt text

  foreach (var piece in Piece)
    {
        if (Vector2.Distance(CurrentColor.transform.position,piece.transform.position)<0.4f &&  piece.GetComponent<Colorslot>().Entered && !CurrentColor.GetComponent<DragDrop>().dragging )
        {
            piece.layer = CurrentColor.layer;
            piece.GetComponent<Colorslot>().ColorInPosition = true;
            CurrentColor.transform.position = piece.transform.position;
            CurrentColor.transform.position=new Vector3(CurrentColor.transform.position.x,CurrentColor.transform.position.y,CurrentColor.transform.position.z-10f);
            CurrentColor.GetComponent<DragDrop>().draglock = true;
            NextColor.transform.position = Vector2.Lerp(NextColorPos.transform.position,CurrentColorPos.transform.position, 0.3F*Time.fixedDeltaTime);
            
            CurrentColor = NextColor;
            CurrentColor.GetComponent<DragDrop>().draglock = false;
            nextcolor = false;
            playerturn = false;
            

        }else   if ( !CurrentColor.GetComponent<DragDrop>().dragging && !piece.GetComponent<Colorslot>().Entered)
            CurrentColor.transform.position =
                Vector2.Lerp(CurrentColor.transform.position, CurrentColorPos.transform.position, 3f);
    }

I’ve created this little example script to give you an idea how to tackle the problem. The way you are doing it now, by checking distance for every tile in loops is very memory heavy. Running these heavy loops is not useful for when you only really need one of the tiles in the grid. This is more used for when you want to change a lot or even all the tiles. Like a ColorReset() for your tiles for instance. So as to only single out a couple objects that we actually interact with, we can use a raycast to see what we are pointing at and figure it out from there.


using UnityEngine;

public class ObjectGrid : MonoBehaviour
{
    public GameObject dragObject;
    public Vector3 dragStartPosition;
    public int previousLayer;
    private void TryDrag()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            if(hit.collider.tag == "Drag")
            {
                dragObject = hit.collider.gameObject;
                dragStartPosition = hit.point;
                previousLayer = dragObject.layer;
                dragObject.layer = LayerMask.NameToLayer("Ignore Raycast");
            }
        }
    }

    private void Place()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            bool canPlace = hit.collider.tag == "Place";
            dragObject.transform.position = canPlace ? hit.point : dragStartPosition;           
            dragObject.layer = previousLayer;
            dragObject = null;
        }
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            TryDrag();
        }

        if (Input.GetMouseButtonUp(0))
        {
            if(dragObject != null)
            {
                Place();
            }            
        }

        if (dragObject != null)
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                Vector3 mousePosition = hit.point;
                mousePosition.y = dragStartPosition.y;
                dragObject.transform.position = mousePosition;
            }
        }
    }
}

You can use this script right away if you have a top down camera, a plane as a background, and 2 objects, one with tag “Place” and one with tag “Drag”. Place the script on an object in the scene. When the mouse button is pressed, a raycast will check if the mouse position is on top of an object with tag “Drag”. If it is, move that object with the mouse as long as the button is pressed. Also move it to another layer so that the raycast won’t hit it while we are holding it. When the button is released, send out a ray again, but now to check if there is a “Place” tagged object. If there is, place that object on the hit.point, else place it back from where you pick it up.