Search code examples
unity-game-engineunity-networking

How to get the connection used as parameter in NetworkIdentity.AssignClientAuthority


Weapon is a child object of the player object, the child object Weapon cant be regard as a part of the player object, right? So, I need to use NetworkIdentity.AssignClientAuthority() to give non-player object local authority.

public class Weapon: NetworkBehaviour 
{
    void Start()
    {
         // how to get the conn?
         GameObject.GetComponent<NetworkIdentity>().AssignClientAuthority(conn);
    }

    void Update()
    {
        CmdShot();
    }

    [Command]
    void CmdShot()
    {
         // shot...
    }
}

Solution

  • The NetworkIndentity component must on the root gameobject, and it can only have one in a prefab.

    So, only the root gameobject have the ClientAuthority to send Command to server.The child object under this gameobject has no ClientAuthority.

    And here is more about the NetworkIndentity.