Question by
Refzlund · Feb 07, 2018 at 11:04 PM ·
c#movementnetworkingmultiplayer
Help! Instantiating prefab on a Network.SpawnWithAuthority doesn't give the host (Server && Client) authority.
As the title says, I am trying to instantiate a prefab on a Network:
[Command]
public void Cmd_SpawnPlayer()
{
characterPrefab.GetComponent<Character>().owner = this;
character = Instantiate(characterPrefab, new Vector3(0, 0, 0), Quaternion.identity);
NetworkServer.SpawnWithClientAuthority(character, connectionToClient);
character.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);
character.transform.SetParent(this.transform, false);
// After creating player, set name.
if (character.GetComponent<Character>().charName == "UNGIVEN")
character.GetComponent<Character>().Rpc_SetName("Demo-" + connectionToClient.connectionId);
}
When this Character Prefab is created, a NetworkController is called:
public MonoBehaviour[] onlyLocal;
public Rigidbody2D rigid;
public void Start ()
{
if (!hasAuthority) {
foreach (var item in onlyLocal) {
item.enabled = false;
}
Destroy(rigid);
}
}
This disables all local MonoBehaviour scripts from the Character, if the client doesn't have authority over it and removes the rigid body (since the transforms position is networked through another script).
If there's 1 server-only, 2 clients, the two clients have the authority over their own Character. They can run around etc. without controlling other players.
IF the server tries to host, it creates same prefab, but doesn't have Authority over it, while the normal clients still have authority over theirs.
Comment