So I have made this really simple player script, which gets its controls from a Hashtable I have set up in a different script. The player always moves to the right and the controls will make it change direction. I have set up 4 different control schemes for 4 different players. When I only have 1 player in the scene it works fine, I can assign the player a player number and the control scheme changes depending on player number. Everything works perfectly. However when I duplicate the player and give them 2 different player numbers, the duplicated player will not move or react to controls.
I get these errors when duplicating the player and starting the game
The 1 and 2 are a Debug.Log I put in to check that they are reciving their playernumbers correctly. However player number 2 gets a NullReference
This is the initialization code that gets executed, this is where I fetch the controls in the main Player.cs.
void Start () {
rigidbody = GetComponent<Rigidbody2D> ();
controls = GetComponent<PlayerControls> ();
//GET CONTROLLERS
Debug.Log(playerNum);
control = controls.getControls(playerNum);
setKeys(control);
}
and here is how the PlayerControls.cs looks like.
void Start () {
//Player1
player1 = new Hashtable();
player1.Add("Up", "a");
player1.Add("Down", "d");
player1.Add("Shoot", "s");
player1.Add("Boost", "w");
//Player2
....
....
}
public Hashtable getControls(int num) {
if(num == 1) {
return player1;
} else if(num == 2) {
return player2;
} else if(num == 3) {
return player3;
} else if(num == 4) {
return player4;
} else {
Debug.Log("Incorrect player number!");
return new Hashtable();
}
}