How do I tie my movement script to only selected objects?,How do I tie my movement script to the my selection script?

I’m trying to make the player only move the selected item and not all at once.
Right now I have a movement script that looks like this:

 void Update()
    {
        if (Input.GetKey(KeyCode.A))
            transform.Rotate(-Vector3.forward * RotateSpeed * Time.deltaTime);
        else if (Input.GetKey(KeyCode.D))
            transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
    }

And a selection script that looks like this:

void MouseInput()
    {
        // When Mouse 0 is pressed
        if (Input.GetMouseButton(0))
        {
            // Raycast from camera to mouse position
            Vector2 raycastPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(raycastPos, Vector2.zero);

            // If a collider is hit
            if (hit.collider != null)
            {
                // Log the name of the hit object
                 Debug.Log(hit.collider.gameObject.name);
                
            }

           if (hit.collider.gameObject.name != null)
            {
                Debug.Log("Im called " + hit.collider.gameObject.name + " and I'm hit and should move");
            }
           

        }

Unity does know when something is selected or “hit” and I have my movement script on all the game objects that need movement right now.

Here try this in 1 script on the objects you want the player to move:

using UnityEngine;
using UnityEngine.EventSystems;

public class RotateObject : MonoBehaviour, ISelectHandler, IDeselectHandler
{
    bool selected = false;
    void Update()
    {
        if (selected)
        {
            if (Input.GetKey(KeyCode.A))
                transform.Rotate(-Vector3.forward * RotateSpeed* Time.deltaTime);
            else if (Input.GetKey(KeyCode.D))
                transform.Rotate(Vector3.forward * RotateSpeed* Time.deltaTime);
        }
    }

    private void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
        {
            EventSystem.current.SetSelectedGameObject(gameObject);
        }
    }

    public void OnSelect(BaseEventData eventData)
    {
        selected = true;
    }

    public void OnDeselect(BaseEventData eventData)
    {
        selected = false;
    }
}

Unless I’m misunderstanding your problem that should work.