Probably this helps. Here's code I made, though I made it for a different purpose (changing owner of vehicle key in game) the key is clientauthority.
The player selects which vehicle's key to give away with selectkey() and transfers it with transferkey() which then invoke a method on raycastselect to get the key receiver player and transfers it to the givekey script.
public class keyselector : NetworkBehaviour
{
public raycastselect rayselect;
public Text keyname;
int ind;
NetworkIdentity currentkey;
public void selectkey()
{
NetworkIdentity x = ClientScene.localPlayer;
Cmdselectkey(x);
}
[Command]
public void Cmdselectkey(NetworkIdentity x)
{
var y = new NetworkIdentity[x.connectionToClient.clientOwnedObjects.Count];
(x.connectionToClient.clientOwnedObjects).CopyTo(y);
Targetselectvehicle(y);
}
[TargetRpc]
public void Targetselectvehicle(NetworkIdentity[] y)
{
ind += 1;
if(ind >= y.Length)
{
ind = 0;
}
currentkey = y[ind];
if(y.Length > 1)
{
if (y[ind] != ClientScene.localPlayer)
{
keyname.text = y[ind].gameObject.name;
}
else
{
keyname.text = "SELECT\nKEY";
}
}
else
{
keyname.text = "U HAVE\nNO KEY";
}
}
public void transferkey()
{
if(currentkey != null && currentkey != ClientScene.localPlayer)
{
rayselect.givekey(currentkey);
}
}
}
public class raycastselect : MonoBehaviour
{
public GameObject rider;
public GameObject ramper;
public GameObject key;
public Text toride;
public void go()
{
RaycastHit trump;
if (Physics.Raycast(transform.position, transform.forward, out trump))
{
enterexit stalin = trump.transform.GetComponent<enterexit>();
if(stalin.player == null)
{
stalin.player = transform.parent.gameObject;
}
if (trump.transform.GetComponent<crash>() && trump.transform.GetComponent<crash>().putin == null)
{
trump.transform.GetComponent<crash>().putin = transform.parent.gameObject;
}
stalin.enter();
}
}
public void givekey(NetworkIdentity vehicle)
{
RaycastHit trump;
if (Physics.Raycast(transform.position, transform.forward, out trump))
{
GetComponentInParent<givekey>().Cmdchangeowner(vehicle, trump.transform.GetComponent<NetworkIdentity>());
}
}
public void ramp()
{
RaycastHit trump;
if (Physics.Raycast(transform.position, transform.forward, out trump))
{
ramp stalin = trump.transform.GetComponentInChildren<ramp>();
stalin.door();
}
}
private void Update()
{
RaycastHit trump;
if (Physics.Raycast(transform.position, transform.forward, out trump))
{
if (trump.transform.GetComponentInChildren<enterexit>())
{
rider.SetActive(true);
toride.text = trump.transform.gameObject.name;
}
else
{
rider.SetActive(false);
}
if (trump.transform.GetComponentInChildren<ramp>())
{
ramper.SetActive(true);
}
else
{
ramper.SetActive(false);
}
if (trump.transform.GetComponentInChildren<raycastselect>())
{
key.SetActive(true);
}
else
{
key.SetActive(false);
}
}
else
{
rider.SetActive(false);
ramper.SetActive(false);
key.SetActive(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class givekey : NetworkBehaviour
{
[Command]
public void Cmdchangeowner(NetworkIdentity vehicle, NetworkIdentity player)
{
vehicle.RemoveClientAuthority();
vehicle.AssignClientAuthority(player.connectionToClient);
}
}