Server has no authority when using "NetworkServer.SpawnWithClientAuthority"

Hey guys!

I use NetworkServer.SpawnWithClientAuthority to spawn a gameobject and give its authority to a specific player, but it doesn’t work when the client is the server/host.

Here is my code to spawn:

[Command]
void CmdSpawnCharacter(int _index){
GameObject _character = (GameObject)Instantiate (characters [_index], transform.position, transform.rotation);
NetworkServer.SpawnWithClientAuthority (_character, gameObject);
}

(gameObject being the player prefab in the network manager)

and I have this function on the gameobject that is created:

void Start () {
if (hasAuthority) {
GetComponent().enabled = true;
}
}

1 Like

as method name "Spawn With Client Authority " . Use NetworkServer.Spawn instead.
Please read document first.
https://docs.unity3d.com/ScriptReference/Networking.NetworkServer.html

this below is what i did.
i don’t know how will spawnFunc which takes GameObject as Second param work.
but i used spawnFunc which takes NetworkConnection as second param.
and make sure before spawning, you register the prefab you want to spawn at lobbyManager.

public void CreateFoxFires()
{
if (isServer)
{
Cmd_CreateFoxFires(connectionToClient.connectionId);
}
else
{
Cmd_CreateFoxFires(connectionToServer.connectionId);
}
}

[Command]
void Cmd_CreateFoxFires(int connectionId)
{
NetworkConnection requesterConn=null;
foreach(NetworkConnection conn in NetworkServer.connections)
{
if (conn.connectionId == connectionId)
{
requesterConn = conn;
break;
}
}
ahriMotion.CreateAndSpawnWFire(connectionToClient);
}

public void CreateAndSpawnWFire(NetworkConnection conn)
{
for (int i = 0; i < 3; ++i)
{
WFires = ahriEffect.Instantiate(“FoxFire”, Vector3.zero, Quaternion.identity).GetComponent();
NetworkServer.SpawnWithClientAuthority(WFires*.gameObject, conn);*
}
}
i know it’s quite late answer.
but hope other who has same issue to get a bit of help from this post

Yeah a bit late, but I’m sure it will help others!

You’re doing a league of legends remake?

yepp
just for my portpolio.

and a bit update here.

i forgot to add comment that
i once put this function ‘CreateFoxFires()’ in Start()
and in some reason it didn’t work, so i put this in coroutine so that this is called few secs later.
and it works so i guess
till Start(), the network setting doesn’t seem to be yet finished for spawning network instance, only assuming not sure.
so what i have done is to call this in coroutine, give it a time to figure out whatever networksetting.
and if you can find some callbackfunction which is call after start(), i think it is worth to try.
cheers

Hey!

Using hasAuthority in the Start() method will return false, however using it in the Update() method will work exactly as you intended.