Search Unity

RemoveClientAuthority causing HandleTransform netID not for a valid player

Discussion in 'Multiplayer' started by crydrk, Jul 12, 2018.

  1. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    I'm trying to make a client grab an object in vr and take authority over the client. I get this error: "HandleTransform netId:22 is not for a valid player"

    Note that I've removed parts of this snippet for clarity in this post.

    Code (CSharp):
    1. private void Grab(string grabSide)
    2.     {
    3.         Grabber grabObject = GrabberLeft;
    4.  
    5.         if (grabSide == "left")
    6.         {
    7.             grabObject = GrabberLeft;
    8.         }
    9.         else if (grabSide == "right")
    10.         {
    11.             grabObject = GrabberRight;
    12.         }
    13.  
    14.         // Release if already grabbing - this is relevant in click to grab mode
    15.         if (grabObject.IsGrabbing)
    16.         {
    17.             // Note that grabObject is another class I have not included - GrabbedObject is a GameObject
    18.             NetworkIdentity grabbedIdentity = grabObject.GrabbedObject.GetComponent<NetworkIdentity>();
    19.             NetworkIdentity NIdentity = GetComponent<NetworkIdentity>(); // This is on my player NetworkBehaviour
    20.  
    21.             CmdRelease(grabObject.GrabbedObject.gameObject, grabObject.GetThrowDirection(), grabObject.GetThrowRotation(), NIdentity, grabbedIdentity);
    22.         }
    23.         else
    24.         {
    25.             Interactable grabbedObject = grabObject.GetRandomCollidingObject ();
    26.  
    27.             if (grabbedObject)
    28.             {
    29.                 if (grabbedObject.IsInteractable())
    30.                 {
    31.                     grabbedObject.Interact (grabObject.transform, NIdentity);
    32.  
    33.                     // Try to cast the Interactable as an InteractableObject - if it is, grab it
    34.                     // TODO: I'm not sure this is the best way to do this, but the player object
    35.                     // has to do the grabbing with UNet, so this is my current workaround
    36.                     InteractableObject grabbable = grabbedObject as InteractableObject;
    37.                     if (grabbable)
    38.                     {
    39.                         grabbable.GetGrabbed(grabObject.transform);
    40.  
    41.                         GameObject GameObjectOfGrabbable = grabbable.gameObject;
    42.                         NetworkIdentity NIdentity = GetComponent<NetworkIdentity>();
    43.                         NetworkIdentity grabbedIdentity = grabbable.GetComponent<NetworkIdentity>();
    44.  
    45.                         CmdGrab(GameObjectOfGrabbable, NIdentity, grabbedIdentity);
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51.  
    52.     [Command]
    53.     private void CmdGrab(GameObject grabbedObject, NetworkIdentity localPlayerID, NetworkIdentity grabbedID)
    54.     {
    55.         InteractableObject grabbable = grabbedObject.GetComponent<InteractableObject>();
    56.  
    57.         // These Debug.Logs return as expected: first empty, then client authority info
    58.         Debug.Log("GrabbedID before: " + grabbedID.clientAuthorityOwner);
    59.  
    60.         grabbedID.AssignClientAuthority(localPlayerID.connectionToClient);
    61.  
    62.         Debug.Log("GrabbedID after: " + grabbedID.clientAuthorityOwner);
    63.     }
    64.  
    65.     [Command]
    66.     private void CmdRelease(GameObject grabbedObject, Vector3 throwDirection, Vector3 throwRotation, NetworkIdentity localPlayerID, NetworkIdentity grabbedID)
    67.     {
    68.         InteractableObject grabbable = grabbedObject.GetComponent<InteractableObject> ();
    69.  
    70.         NetworkIdentity grabbedID2 = grabbedObject.GetComponent<NetworkIdentity>();
    71.  
    72.         // These Debug.Logs return as expected: first client authority info, then empty
    73.         Debug.Log(grabbedID.clientAuthorityOwner);
    74.         grabbedID.RemoveClientAuthority(localPlayerID.connectionToClient); // I've also tried this is just 'connectionToClient'
    75.         Debug.Log(grabbedID.clientAuthorityOwner);
    76.  
    77.         // === IT IS HERE THAT I GET THE ERROR, EVEN WITHOUT ANY MORE CODE FOLLOWING
    78.  
    79.     }
     
  2. Vegoo89

    Vegoo89

    Joined:
    May 11, 2018
    Posts:
    6
    This should work, I have similar example in my project and its working for me this way:

    if(grabbedID.clientAuthorityOwner != null) {
    grabbedID.RemoveClientAuthority(grabbedID.clientAuthorityOwner);
    }
     
  3. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    Thanks for the reply!

    Looks like it's properly hitting the RemoveClientAuthority, but now I'm getting a different warning:

    HandleTransform netId:39 is not for a valid player

    Updated snippet:

    Code (CSharp):
    1.  
    2. NetworkIdentity grabbedNetworkID = grabbedObject.GetComponent<NetworkIdentity>();
    3.  
    4.         // Remove client authority
    5.         NetworkConnection currentOwner = grabbedNetworkID.clientAuthorityOwner;
    6.  
    7.         Debug.Log("currentOwner: " + currentOwner);
    8.  
    9.         if (currentOwner != null)
    10.         {
    11.             grabbedNetworkID.RemoveClientAuthority(currentOwner);
    12.         }
    13.         else
    14.         {
    15.             Debug.LogWarning("currentOwner was null!");
    16.         }
    17.  
    I'm spawning this object with NetworkServer.Spawn at runtime, and that spawned object is showing up across all clients. Not sure why I would have this error if the rest is showing up properly.

    I would expect the object it's failing to release would have the scene ID 39 in the inspector for the NetworkIdentity, but it's 0 instead. In fact, every object I spawn over the network has a "unique" ID of 0.

    Here is the test script from which I'm spawning the objects:
    Code (CSharp):
    1. public class PrefabSpawner : NetworkBehaviour
    2. {
    3.     public GameObject PrefabToSpawn;
    4.     public bool CopyRotation = true;
    5.  
    6.     public float Delay = 5f;
    7.  
    8.     private void Awake()
    9.     {
    10.  
    11.         Invoke("SpawnAfterTime", Delay);
    12.        
    13.     }
    14.  
    15.     private void SpawnAfterTime()
    16.     {
    17.         if (!isServer)
    18.             return;
    19.  
    20.         GameObject SpawnedObject = GameObject.Instantiate(PrefabToSpawn);
    21.         SpawnedObject.transform.position = transform.position;
    22.  
    23.         if (CopyRotation)
    24.         {
    25.             SpawnedObject.transform.rotation = transform.rotation;
    26.         }
    27.  
    28.         NetworkServer.Spawn(SpawnedObject);
    29.     }
    30. }
     
    Last edited: Jul 15, 2018
  4. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    Turns out the root of my problem is from elsewhere in the script. When I grab an object, I set some variables and change the parent of the object so that it can be held by the player. Turning this on and off reliably gives me the not a valid player warning.

    I had this working in some way previously. Is it wrong to change a parent on just one client while it has authority, and let the network transform component handle that position? It seems the only other option is to put the object underneath each client's instance of that hand, but that seems like overkill and would fight with the network transform.