- Home /
Can't get GameObjects to display on client upon connecting to server
I'm trying to create a text based multiplayer game but have ran into some problems with the basics of networking.
The scenario here is that if player "A" attacks and beats player "B", "B" will get hospitalized and a prefab will be instantiated on the approriate interface. This itself works fine. But, if a player connects to the server the already hospitalized players will somehow to be loaded and displayed for this player as well which I have been unsuccesful at doing, and I'm not really sure why.
Here's the code for hospitalizing players;
[Command]
void CmdHospitalizePlayer() {
HospitalizedPlayer = HospitalizedPlayerPrefab;
HospitalizedPlayer.name = PlayerName;
if (!(GameObject.Find("/Canvas/LeftPanel/HospitalPanel/HospitalizedPanel/" + HospitalizedPlayer.name))) {
RpcHospitalizePlayer();
} else {
Debug.Log(HospitalizedPlayer.name + " is already hospitalized.");
}
}
[ClientRpc]
void RpcHospitalizePlayer() {
HospitalPanel = GameObject.Find("HospitalizedPanel");
PlayerNameText.text = PlayerName;
HospitalizedPlayer = Instantiate(HospitalizedPlayerPrefab);
HospitalizedPlayer.name = PlayerName;
HospitalizedPlayer.transform.SetParent(HospitalPanel.transform);
NetworkServer.SpawnWithClientAuthority(HospitalizedPlayer, connectionToClient);
}
And here is the desperate code that I'm running at Start() to try to load hospitalized players;
[ClientRpc]
void RpcLoadHospitalizedPlayers() {
HospitalizedPanel = GameObject.Find("HospitalizedPanel").transform;
int children = HospitalizedPanel.childCount;
for (int i = 0; i < children; i++) {
GameObject toSpawn = HospitalizedPanel.GetChild(i).gameObject;
Debug.Log("Found child: " + HospitalizedPanel.GetChild(i));
if (hasAuthority) {
GameObject HospitalizedPlayer = Instantiate(toSpawn);
HospitalizedPlayer.transform.SetParent(HospitalizedPanel);
} else
Debug.Log("No authority.");
}
}
The hospitalized prefabs do get loaded when a new client connects, but for some reason only for the host and not the connecting client.
Apologies for the formatting in this thread and if I forgot to include any relevant details, please feel free to ask for them.