Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UNet | I'm having trouble assigning authority to game objects

Discussion in 'UNet' started by McGunn, Dec 26, 2017.

  1. McGunn

    McGunn

    Joined:
    Aug 7, 2012
    Posts:
    33
    I'm having serious issues assigning authority to units in my RTS prototype using UNet.

    The Player Object has a component that sends a command to the server to spawn units and assign authority to the client. However once the units are spawned, even after OnStartAuthority() is called, every client and the server all have authority over every unit.

    The units and player objects all have NetworkIdentity with Local Player Authority Checked in. (However for many hours I've tested it with all combinations of that checked on and off)

    The Spawner code on the Player Object is:


    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.Networking;
    5.     public class SpawnUnits : NetworkBehaviour {
    6.  
    7.         public GameObject[] units;
    8.  
    9.         public override void OnStartServer()
    10.         {
    11.             if (isLocalPlayer)
    12.                 Cmd_SpawnUnitsMethod();
    13.         }
    14.  
    15.     [Command]
    16.         public void Cmd_SpawnUnitsMethod()
    17.         {
    18.             Vector3 pos = transform.position;
    19.             foreach(GameObject obj in units)
    20.             {
    21.            
    22.                 GameObject inst = Instantiate(obj, pos, Quaternion.identity);
    23.                 Debug.Log(connectionToClient);
    24.                 NetworkServer.SpawnWithClientAuthority(inst,connectionToClient);
    25.                 pos += Vector3.right * 2;
    26.             }
    27.         }
    28.     }
    And the code on the units that make changes depending on their authority is as follows:


    Code (CSharp):
    1. public class NetworkedUnit : NetworkBehaviour {
    2.     public override void OnStartAuthority()
    3.         {
    4.         Set();
    5.         }
    6.  
    7.      public void Set()
    8.         {
    9.             if (!hasAuthority)
    10.             {
    11.                 Debug.Log("Turning off non-authority stuff");
    12.                 GetComponent<UnitBase>().enabled = false;
    13.                 GetComponent<NavMeshAgent>().enabled = false;
    14.                 disabled = true;
    15.             }
    16.         }
    And what I've found is that no Unit ever has `hasAuthority==false` ever. I've scoured Google for many hours to see what I'm doing wrong, and I've tried many variations of the code including using `AssignClientAuthority(Networking.NetworkConnection conn);`

    Help would be much appreciated!
    Thanks!
     
    Last edited: Dec 27, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How are you checking if a client has authority over a GameObject? You don't seem to be doing any checking anywhere hasAuthority could actually be false.

    OnStartAuthority() is only supposed to be called if that client has authority over the GameObject, but then you just call Set which then checks "if (!hasAuthority)" which should never be the case. So that part is pretty confusing to me, since Set() should always just do nothing if that is the only place you ever call it.

    I'd try using SpawnWithClientAuthority, and then in your NetworkUnit script just call Set() from Start(), and add a debug message if hasAuthority is true so you have debug messages for both false and true states.

    https://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.OnStartAuthority.html
     
  3. McGunn

    McGunn

    Joined:
    Aug 7, 2012
    Posts:
    33
    Oh thank you, I was unaware of StartAuthority only running on the client with authority.
    I believe I've tried running it in Start, but it seems to take about 6 frames before the instance has it's authority applied, and so everything returns false. This is the kind of info I needed to point me in the right direction, and I'll reply with the working setup once I get that working. Thanks a lot.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Ah, sorry I wasn't aware that when Start is called that client authority might not already be set. (In the game I'm working on I use server authority for everything, so haven't played much with client authority)

    Hope you figure it all out!
     
  5. HideOnMada

    HideOnMada

    Joined:
    Jan 23, 2020
    Posts:
    2
    I'm having the same trouble. Recently started doing networking I'm not sure how I'm supposed to go around the issues.