Assign/Remove Client Authority through Trigger[Solved]

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 ().netId;
GameObject client = NetworkServer.FindLocalObject (nIns);
NetworkIdentity ni = client.GetComponent ();
ni.AssignClientAuthority(connectionToClient);
}

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

}

Not sure I’m having a similar issue, FindLocalObject can return null though and it might not have an authoritative owner.

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);
}

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().connectionToServer.connectionId.ToString());

CmdAssignAuthority();
}

[Command]
void CmdAssignAuthority()
{
GameObject assignAuthorityObj = GameObject.Find(“Ball”);
// assignAuthorityObj.GetComponent().AssignClientAuthority(GetComponent().connectionToClient);
assignAuthorityObj.GetComponent().AssignClientAuthority(this.GetComponent().connectionToClient);

}
}

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.

3238723–248844–Athor.cs (1.58 KB)

This version you can assign/remove client authority of any object just entering and exiting the player trigger zone.

3238940–248887–Athor.cs (1.47 KB)

1 Like

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”.

1 Like

@Tom-Mensink1 Glad the script helped. I also took your advice and updated the script.

Updated Script Below.

3388848–266396–Athor.cs (1.48 KB)

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 ?

1 Like

Guys, I have been looking for this for days. Thank you ever EVER so much. I really appreciate this!

How about lags on NetworkTransform and weird teleports while assigning/removing Client Authority? Has anyone encountered such behaviour?

I’ve been encoutering this teleporting behaviour. Did you find a solution?