accessing components C# / a custom input manager

Hi All,
I’m trying to create a custom input manager, where I can change the input device on the fly. I’m not very experienced in Unity or C# so my angle/way to tackle the issue might not be the best…not to mention completely wrong.

The way I have set it up at the moment is that the player Script queries the InputManager.cs for input. The idea is that the input manager gathers the input from whatever device/control mapping that is choosen… and then the “player” can in turn access the input manager for the input.

Declaring the input devices:

//The Joystick
private JoystickScript theJoystick;
private GameObject theJoystickGameObject;

//The Keyboard
private KeyboardInputScript theKeyboard;
private GameObject	theKeyboardGameObject;

Hook them up:

//Find  it by name
currentInputDeviceGameObject = GameObject.Find("Joystick");
		
//"create" a pointer to it
theJoystick = currentInputDeviceGameObject.GetComponent<JoystickScript>();

Get Input:

void Update()
{
	//Get the input from the joystick
	y=theJoystick.inputY;
}

Change a input device:

void SetInputDevice(inputDeviceType theNewInputDevice)
{
  Do stuff
}

The way I access the input in the update function is not flexible, as the component type needs to be determined i.e (JoystickScript or KeyboardScript)

I prefer not to be checking which device to select the input from in the update function with a conditional statement/switch as that would be a waste of “time”.

Any suggestions on how I could access the input in the update function in a more flexible way…so instead of “joystick.inputX” i could have “inputDevice.inputX” where the input device can be switched to accelerometer/mouse/keybpard etc…

Maybe I’m on the wrong path of to setup something like this in a good manner…

As I mentioned I’m quite new to unity so I have’t reached the enlightened state of knowing the best design patterns and practices to put together something in unity, any help would be much appreciated.

Thanks in advance!

Thanks to both of you,
I went with subclassing/inheritance to solve the problem. With unity input manager you can create more input “Events” but then you need to listen to different Events with the different “devices”…at least as far I understood it…need to look more into it at some point.

So what I did was create a common inputdevice class(no methods just the public variables I needed to access)

public class InputDevice : MonoBehaviour

{
public float inputX;
public float inputY;

 public float inputClamp = 1f;
 public float sensitivity = 1f;

}

The input devices were created with InputDevice as base class instead of monobehaviour:

public class AccelerometerScript : InputDevice

public class MouseJoystickScript : InputDevice

Then I access them by “casting” the joystick/accelerometer as the generic InputDevice

theInputDevice=theJoystick as InputDevice;

x=theInputDevice.inputX

So this seems to work, if this is best practice or not… that I do not know… but it works! Thanks again! :slight_smile: