How can I change the targetPosition for each game object individually?

Hello Unity community, I’m currently developing a game where you play as an air traffic controller trying to crash planes. You move planes by clicking on them then clicking on the map for where they should go. However, I have encountered a problem. Whenever I click on a plane It sets the target position the same for all the planes, Is there any way I can make it so it only changes it for the plane I have selected? Or maybe is there any other way I can approach this?

Here is my code for the plane logic

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

public class PlaneMovement : MonoBehaviour
{
    [Header("References")]
    [SerializeField] private Transform targetPosition;


    [Header("Attributes")]
    [SerializeField] private float planeSpeed = 5f;
    [SerializeField] private float stoppingDistance = 0.1f;

    private Rigidbody2D rb2d;
    private bool isPlaneSelected = false;
   

    private void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();

    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (isPlaneSelected)
            {
                SetPlaneTarget();
            }
            else
            {
                RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
                if(hit.collider != null && hit.collider.CompareTag("Plane"))
                {
                    SelectPlane();
                }
            }
        }

        //Movement logic goes here :

        Vector3 direction = (targetPosition.position - transform.position).normalized;

        rb2d.velocity = direction * planeSpeed;

        float distance = Vector3.Distance(transform.position, targetPosition.position);

        if (distance <= stoppingDistance)
        {
            rb2d.velocity = Vector3.zero;
        }

        //Rotation logic goes here :

        transform.up = direction;

    }

    private void SelectPlane()
    {
        if(isPlaneSelected)
        {
            DeselectPlane();
        }

        isPlaneSelected = true;
    }


    private void SetPlaneTarget()
    {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = -Camera.main.transform.position.z;

        Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
        worldPosition.z = 0f;

        targetPosition.position = worldPosition;

        isPlaneSelected = false;
    }
       

    private void DeselectAllPlanes()
    {
        PlaneMovement[] allPlanes = FindObjectsOfType<PlaneMovement>();
        foreach(PlaneMovement plane in allPlanes)
        {
            plane.DeselectPlane();
        }
    }

    private void DeselectPlane()
    {
        isPlaneSelected = false;
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(transform.position, targetPosition.position);
    }
}

You can see in the images below that whenever I click on a plane and then on the map It changes the target position for both planes


Your target position is a Transform, so if you create your planes sharing this Transform, what you’ll get is what you described that they all share the same. Change the type to just Vector2 to make it individual.

You are getting tangled up between the Plane instance (each one) and a Plane Manager.

For instance, most of your class deals with Plane instance, except for DeselectAllPlanes, which is definitely more of a manager.

This results in the weird code smell on line 64 where I was puzzled why a plane being selected would first deselect. That is nothing that a Plane would ever do, only a Plane Manager.

The Plane manager is probably the best place for the one point of truth for which plane is currently selected. That’s almost always how these things are done. Otherwise two different planes could have different notions of who they think is selected.

In addition to above, it’s 2D so if you want to check if a point overlaps a collider, use OverlapPoint. Don’t use a raycast with a degenerate distance of zero. Not only is it faster but it’s less verbose an much clearer on intent.

Also, consider using layers as filters and not tags. That way you can query for specific things without having to additionally check in script.

Manager:

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

public class PlaneManager : MonoBehaviour
{
    void Update()
    {
        // if a user clicks on a plane then set the plane's destination
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.forward,out hit,1000))
            {
                Plane p=hit.transform.GetComponent<Plane>();
                if (p)
                {
                    p.destination=p.transform.position+Random.insideUnitSphere*3; // set the plane's destination
                }
            }
        }
    }
}

Plane:

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

// Place this script on all the planes
public class Plane : MonoBehaviour
{
    public Vector3 destination; // the plane's destination

    void Start()
    {
        destination=transform.position;
    }

    void Update()
    {
        transform.Translate((destination-transform.position)*0.1f); // Move to destination/target
    }
}