- Home /
[Solved]Two Player Error
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();
}
}
On Player.cs line 95 is this:
up$$anonymous$$ey = (string) controller["Up"];
the whole function is:
void set$$anonymous$$eys (Hashtable controller) {
up$$anonymous$$ey = (string) controller["Up"];
down$$anonymous$$ey = (string) controller["Down"];
shoot$$anonymous$$ey = (string) controller["Shoot"];
}
I don't know why, but it works now :p I closed unity and when I tried what you commented it worked :P
Answer by Ibzy · Jun 23, 2015 at 02:23 PM
Looks like it isn't sending a Hashtable in the setKeys() call.
Try putting a check around it like this:
if(control!=null){
setKeys(control);
} else {
Debug.Log("No Control");
}
And see what comes out?
Your answer
![](https://koobas.hobune.stream/wayback/20220613201315im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Local Multiplayer Camera 0 Answers
SmartFox Objects?? 0 Answers
Setting different values on editor and build 1 Answer
Duplicate Object, Lose Animation 0 Answers
MultiplayerEventSystem Same UI BUG 0 Answers