- Home /
Question by
Jack-Does · Mar 17, 2018 at 05:10 PM ·
networkingarrayslists
How to handle local arrays in unet?
I am working on my first multiplayer game and having trouble with an list of gameobjects. Each player has 2 preset arrays of game objects. when they start, each player chooses their team and an empty list is filled with the corresponding preset array objects.
The problem is that only the host is able to fill the empty list while the client just twiddles their thumbs. Nothing should need to be sent over the network sense all the players have both preset arrays built in from the get-go.
public List<GameObject> selectedArmy;
public GameObject[] armyRed;
public GameObject[] armyBlue;
void Update(){
if (!isLocalPlayer) {
return;
}
if (!armySelected) {
if(Input.GetKeyUp(KeyCode.R)){
foreach(GameObject go in armyRed){
selectedArmy.Add (go);
}
armySelected = true;
}
else if (Input.GetKeyUp(KeyCode.B)){
foreach(GameObject go in armyBlue){
selectedArmy.Add (go);
}
armySelected = true;
}
}
// if(isTurn){
if(Input.GetKeyUp(KeyCode.Q)){
CmdSpawnModel (0);
}
if(Input.GetKeyUp(KeyCode.E)){
CmdSpawnModel (1);
}
// }
}
[Command]
public void CmdSpawnModel(int _unitToSpawn){
GameObject _newUnit = (GameObject)Instantiate (selectedArmy[_unitToSpawn], new Vector3(0,0,0), transform.rotation);
NetworkServer.Spawn (_newUnit);
}
All I get from the client is an error saying that the array index is out of range. The host handles everything fine.
I am new to networking so maybe I'm just overlooking something simple. Any help is greatly appreciated.
P.S. everything has networkidentity, is added to networkmanagers registered prefabs. And if i change the code to not use arrays everything works.
Comment
where and how is a client throwing the error? the only instance accessing that array is the server aka host.