take every object separately

Hi this is my code :

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

public class TouchMoove : MonoBehaviour
{

    void Start()
    {

    }
    void Update()
    {
        if (Input.touchCount > 0 /*&& Input.GetTouch(0).phase == TouchPhase.Began*/)
        {
            this.GetComponent<SpriteRenderer>().color = Color.red;
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
            
            if (hit.collider != null)
            {
                this.GetComponent<SpriteRenderer>().color = Color.blue;

                    Touch touch = Input.GetTouch(0);
                    if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
                    {
                    this.GetComponent<SpriteRenderer>().color = Color.green;
                    Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
                        transform.position = touchedPos;
                    }


            }


        }
    }
}

when i am dragging an object with my finger they are all coming under my finger, I want to tell the code to move only the one i have touched.
All my object have this script…
thanks a lot for your help :slight_smile:

Try to change this line:

     if (hit.collider != null)

to

      if (hit.collider.gameObject == this.gameObject)

thanks it works :slight_smile: