Search Unity

Unable To Spawn Player Units With Authority

Discussion in 'Multiplayer' started by Outer_Galactic, Dec 12, 2019.

  1. Outer_Galactic

    Outer_Galactic

    Joined:
    May 20, 2019
    Posts:
    3
    I am having a problem spawning player units from the connection objects.
    Currently my network manager Spawns a connection object for each player which is mostly empty and stores global variables like kills and such. When i attempt to spawn a player unit through it, the new object doesn't have authority so that i may control it.

    Original call:
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (!isLocalPlayer)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         if (!playerAlive)
    9.         {
    10.             if (Input.GetKeyDown(KeyCode.Space))
    11.             {
    12.                 CmdSpawnMyUnit(IDRef);
    13.                 playerAlive = true;
    14.                 Debug.Log("spawning player with " + IDRef + " ID");
    15.             }
    16.         }
    17.  
    18.     }
    19.  
    20.  
    21.     [Command]
    22.     void CmdSpawnMyUnit(string _owningID)
    23.     {
    24.         GameObject _go = Instantiate(playerUnit);
    25.  
    26.         NetworkServer.SpawnWithClientAuthority(_go, connectionToClient);
    27.         RpcSetOwnership(_go, _owningID);
    28.     }
    I was under the impression that this code would give me a "playerUnit" with authority but the server spawns the player without authority, even though every object in question has Local Client Authority checked on their respective network identity components.

    For reference the "RpcSetOwnership()" is completely unrelated to local client authority, and I am aware of that. Additionally my Instantiate has only one argument because i am just testing basic functionality.

    Also I'm unsure if this information will be relevant but the client that i am trying to spawn players with is also the host Server of the game, that being said even separate client instances still have these issues.

    Thank you in advance for the assistance
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Where are you checking what client actually has authority over the spawned object?

    On your second to last statement, my understanding is when you're trying to spawn networked objects with the host having authority, you don't use SpawnWithClientAuthority at all.
     
  3. Outer_Galactic

    Outer_Galactic

    Joined:
    May 20, 2019
    Posts:
    3
    I'm not exactly sure what you mean, if you mean line 26
    Code (CSharp):
    1. NetworkServer.SpawnWithClientAuthority(_go, connectionToClient);
    i thought "connectionToClient" was an appropriate identifier for the client sending the request, even if the client is also the host. (Though this function does not work for connected client instances either)

    unless you meant where I'm actually doing the "hasAuthority" check, which would be in a Start method on a script of the spawned player. which is just basically "if (hasAuthority) { do local player stuff } else { Spawn the boy but don't send inputs to whats not ours }". I can paste the code block for this as well if you would like but i felt is was unrelated.

    also in regards to this "my understanding is when you're trying to spawn networked objects with the host having authority, you don't use SpawnWithClientAuthority at all.". I'm unsure what else i should be doing then.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah, hasAuthority is Start should be fine.

    It was in reference to this statement:

    Is the host really a client in this context? If you're trying to spawn the object with host authority in this specific instance, try just using Spawn instead.

    Personally though, dealing with authority in Unet spread across all the clients can be a mess. You're probably better off just switching everything over to host authority, and send Commands to the host whenever the client wants to manipulate their own objects. If the response time is too slow, implement some client side prediction. Also, Unet was never finished and ended in a still buggy state, even before it was officially deprecated. I'd consider switching network API's.
     
  5. Outer_Galactic

    Outer_Galactic

    Joined:
    May 20, 2019
    Posts:
    3
    This is upsetting, Are you actually telling me that there Isn't anything wrong with this code and it would be easier to restructure because I'm unlikely to get around it? I really don't want to do that, I believe it will kill my momentum with this project. :(
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Looks like if you use SpawnWithClientAuthority for the host, hasAuthority doesn't work properly in Start().

    https://forum.unity.com/threads/has...-after-using-spawnwithclientauthority.391781/

    What I was saying is there have been lots of threads regarding issues people have seen when using client authority. None of this stuff got fixed, or will ever be fixed. Development essentially ended for Unet around 4 years ago. So you've got to work around the issues one at a time, design your game so you avoid many of the issues (host/server authority over everything is known to be less problematic for Unet), or switch to a network API which is currently being supported.

    You're going to be encountering more bugs, and the number of people responding to Unet questions this far after it was deprecated is shrinking quickly.