Search Unity

Assign/Remove Client Authority through Trigger[Solved]

Discussion in 'Multiplayer' started by MiddleEye, Sep 18, 2017.

  1. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    Hi I'm having a problem assigning authority to clients. The Host can remove the authority of an object spawned by the client but can not regain authority.

    On the host side, when removing authority I get "HandleTransform netId:3 is not for a valid player (UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate())

    Here's my full script attached to the basketball.


    using UnityEngine;
    using System.Collections;
    using UnityEngine.Networking;

    public class ObjectAuthorityToggle : NetworkBehaviour {

    public GameObject obj;


    void Update ()
    {
    if (!isLocalPlayer)
    return;

    if (Input.GetKeyDown (KeyCode.P))
    {
    Cmd_RemoveLocalAuthority ();
    Cmd_AssignLocalAuthority ();

    }

    if (Input.GetKeyDown (KeyCode.L))
    {

    Cmd_RemoveLocalAuthority ();
    }

    }





    [Command]
    void Cmd_AssignLocalAuthority () {
    NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity> ().netId;
    GameObject client = NetworkServer.FindLocalObject (nIns);
    NetworkIdentity ni = client.GetComponent<NetworkIdentity> ();
    ni.AssignClientAuthority(connectionToClient);
    }

    [Command]
    void Cmd_RemoveLocalAuthority () {
    NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity> ().netId;
    GameObject client = NetworkServer.FindLocalObject (nIns);
    NetworkIdentity ni = client.GetComponent<NetworkIdentity> ();
    ni.RemoveClientAuthority (ni.clientAuthorityOwner);
    }



    }
     
    Last edited: Sep 18, 2017
  2. js3263854

    js3263854

    Joined:
    Jan 26, 2017
    Posts:
    8
    Not sure I'm having a similar issue, FindLocalObject _can_ return null though and it might not have an authoritative owner.
     
    Last edited: Sep 18, 2017
  3. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    okay, i want to try the following, but how do I call it without getting an over-ride error?

    [Command]
    void CmdAssignObjectAuthority(NetworkInstanceId netInstanceId)
    {
    // Assign authority of this objects network instance id to the client
    NetworkServer.objects[netInstanceId].AssignClientAuthority(connectionToClient);
    }

    [Command]
    void CmdRemoveObjectAuthority(NetworkInstanceId netInstanceId)
    {
    // Removes the authority of this object network instance id to the client
    NetworkServer.objects[netInstanceId].RemoveClientAuthority(connectionToClient);
    }
     
  4. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    Now my problem is "reference not set to an instance of an object".

    So far I have this script attached. attached to the object:

    using UnityEngine;
    using UnityEngine.Networking;
    using System.Collections;

    public class ObjectAuthority : NetworkBehaviour {

    private LocalAuthority LocAuthor;
    public NetworkIdentity ThisItem;
    public NetworkInstanceId ThisItem2;



    public void OnTriggerStay(Collider other)
    {
    if (other.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.F))
    {

    LocAuthor.assignAuth();
    }
    }
    }

    ////////////////////////////////
    and this script attached to the local player:


    using UnityEngine;
    using UnityEngine.Networking;
    using System.Collections;

    public class LocalAuthority : NetworkBehaviour {



    public void assignAuth()
    {
    if (!isLocalPlayer)
    return;

    Debug.Log("IDs: " + this.GetComponent<NetworkIdentity>().connectionToServer.connectionId.ToString());

    CmdAssignAuthority();
    }

    [Command]
    void CmdAssignAuthority()
    {
    GameObject assignAuthorityObj = GameObject.Find("Ball");
    // assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(GetComponent<NetworkIdentity>().connectionToClient);
    assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);

    }
    }
     
  5. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    Using Unity 5.42 Okay Here's the full script that is attached to the player that can assign client authority and remove client authority. Ball is in reach of player trigger and the player presses F, the ball will be assigned to that player. When the ball leaves the player trigger, the ball will be removed from the player authority.
     

    Attached Files:

    • Athor.cs
      File size:
      1.6 KB
      Views:
      1,343
  6. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    This version you can assign/remove client authority of any object just entering and exiting the player trigger zone.
     

    Attached Files:

    • Athor.cs
      File size:
      1.5 KB
      Views:
      1,582
    Tom-Mensink likes this.
  7. Tom-Mensink

    Tom-Mensink

    Joined:
    Jul 27, 2017
    Posts:
    17
    That's really great @MiddleEye , finally got it working too, thanks to your script.
    Just a minor thing, there is a small error in your script below: "connectionToClient" should be "playerID.connectionToClient".
     
    MiddleEye likes this.
  8. MiddleEye

    MiddleEye

    Joined:
    Feb 9, 2017
    Posts:
    16
    @Tom-Mensink Glad the script helped. I also took your advice and updated the script.

    Updated Script Below.
     

    Attached Files:

    • Athor.cs
      File size:
      1.5 KB
      Views:
      1,976
  9. JulienSVirtu

    JulienSVirtu

    Joined:
    Mar 7, 2018
    Posts:
    2
    Hi,
    I trying to solve a problem with AssignClientAuthority to provide the control of a scene object to a player. I'm pretty sure it was working on another project in 5.4 but on 2017.4.2 with another project (but same code), it works but as soon as a new connection (new player) arrive, all assigned client authority seems to be removed. Is it a bug ?
     
    IthonyGames likes this.
  10. Graithen

    Graithen

    Joined:
    Nov 17, 2015
    Posts:
    4
    Guys, I have been looking for this for days. Thank you ever EVER so much. I really appreciate this!
     
  11. orzech123

    orzech123

    Joined:
    Mar 12, 2018
    Posts:
    17
    How about lags on NetworkTransform and weird teleports while assigning/removing Client Authority? Has anyone encountered such behaviour?
     
  12. alexadkins

    alexadkins

    Joined:
    Aug 29, 2017
    Posts:
    2
    I've been encoutering this teleporting behaviour. Did you find a solution?