How to use one script for input on multiple objects?

I’ve made a class which handles basic input.

public class ControllerInput
	
{
	
		public bool moveLeft;
		
		public void MoveLeft(){
		if (Input.GetKeyDown(KeyCode.A)){
				moveLeft = true;
			}
		}
		
	//Constructor
		public ControllerInput(){
			moveLeft = false;
		}
	}

Now I have two cubes in game, named player and player.
I want to have a script which would call the command to read input and do something with the cube. In this case - change its location.

public ControllerInput x = new ControllerInput();//An object is made
	
	void Start () {
		//x.MoveLeft();
		//x.moveLeft = true;
	}
	
	
	void Update () {
		x.MoveLeft();
		if(x.moveLeft){
		Debug.Log("Works!");
		transform.position = new Vector3(0, 0, 0);
		}
	}

The problem is - I’m not sure how to do that there would only be one script that I would attach to both of the cubes, but that would read input differently - if I press A - cube changes position. If I press B - cube changes position.

It’s very important that the script would still remain the same, since it is used by both objects.

The standard way to customise scripts for multiple objects is to use public variables that are assigned via the editor. Change your code to the following.

public class ControllerInput {

    public bool moveLeft;
    public KeyCode CustomKey;

    public void MoveLeft(){
        if (Input.GetKeyDown(CustomKey)){
            moveLeft = true;
        }
    }
 
    //Constructor
       public ControllerInput(){
         moveLeft = false;
    }
}

Then you could assign the key to each object from the editor. Or you could use another script to assign it.

Note: I’ve never actually tried this, but the concept should be sound. Let me know if it actually works