Search Unity

Server has no authority when using "NetworkServer.SpawnWithClientAuthority"

Discussion in 'Multiplayer' started by Nain-Maboul, Apr 6, 2017.

  1. Nain-Maboul

    Nain-Maboul

    Joined:
    Mar 14, 2016
    Posts:
    11
    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<PlayerController>().enabled = true;
    }
    }
     
    Lushlush likes this.
  2. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    201
  3. mynameisjun

    mynameisjun

    Joined:
    Dec 9, 2017
    Posts:
    2
    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<AhriFire>();
    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
     
  4. Nain-Maboul

    Nain-Maboul

    Joined:
    Mar 14, 2016
    Posts:
    11
    Yeah a bit late, but I'm sure it will help others!

    You're doing a league of legends remake?
     
  5. mynameisjun

    mynameisjun

    Joined:
    Dec 9, 2017
    Posts:
    2
    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
     
  6. Lushlush

    Lushlush

    Joined:
    Oct 20, 2018
    Posts:
    1
    Hey!

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