Search Unity

hasAuthority returns false on host after using SpawnWithClientAuthority.

Discussion in 'Multiplayer' started by forcepusher, Mar 15, 2016.

  1. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    Code (CSharp):
    1. private void OnServerAddPlayer(NetworkConnection networkConnection, short controllerId)
    2. {
    3.     var go = (GameObject)Instantiate(_vehicle, _spawnPoint.position, _spawnPoint.rotation);
    4.     NetworkServer.SpawnWithClientAuthority(go, networkConnection);
    5. }
    So "hasAuthority" on that new gameobject is always false on host, but true on clients connected to the host.
    I know that this is an expected behavior as stated in http://docs.unity3d.com/Manual/UNetSpawning.html
    But this "(and on the host)" breaks the whole "same code work on host, server and client" paradigm that uNet has. This property was probably rushed, because host should be considered as a client and server at the same time.
    Anyway, did anybody came up with a good workaround ?
     
  2. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    Never mind, I figured out what's wrong. This is not an expected behavior (misunderstood manual text).
    The thing is that I am not supposed to use hasAuthority immediately in Start() on host after spawning the object. Gotta use override-able NetworkBehaviour methods like OnStartAuthority for network-related initialization.
     
    Last edited: May 25, 2016
  3. isidro02139

    isidro02139

    Joined:
    Jul 31, 2012
    Posts:
    72
    Wow this post REALLY REALLY helped me, thank you!!
     
  4. MirkoMk5

    MirkoMk5

    Joined:
    Sep 1, 2013
    Posts:
    3
    I've the same problem. I want spawn the player manually. After i run the scene with StartHost() or StartClient(), Unity tell me that the player object haven't a local authority... How i can resolve this issue?
     
  5. MKMW

    MKMW

    Joined:
    May 23, 2017
    Posts:
    1
    Thanks very much! I've been trying to find and solving this bug for like 2 days.
     
  6. justresolvejr

    justresolvejr

    Joined:
    Sep 13, 2020
    Posts:
    4
    can you give a code example because I did that on mine and it still doesn't work.