Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Parenting error in Netcode for GameObjects

Discussion in 'Netcode for GameObjects' started by AngelHYYPE09, Dec 22, 2023.

  1. AngelHYYPE09


    Jan 31, 2022
    Hi there. So i have a prefab that gets instantiated in the network and if it collides with an object with the tag "PizzaLay", will parent with that object and also make the object itself kinematic.
    Everything works fine except the parenting, the host can do it perfectly, but the client can't since it drops this error:
     NotServerException: Only the server can reparent NetworkObjects 

    I need both Client and Host be able to do that, i know that only the "server" can do it and for that reason i used ServerRpc and even ClientRpc to execute the parenting code, none of them work though.
    Any wonder of what's going on? Here's the code:
    Code (CSharp):
    1.  public void OnCollisionEnter(Collision col)
    2.     {
    3.         if (col.gameObject.tag == "PizzaLay")
    4.         {
    5.             PizzaPub = col;
    6.             countinglife = false;
    7.             lifetime = 0f;
    8.             PlayerMovementSOLO.inst.IngCount++;
    9.             AssignParentServerRpc();
    10.             AssingParentClientRpc();
    12.         }
    14.     }
    16.     [ServerRpc(RequireOwnership = false)]
    17.     public void AssignParentServerRpc()
    18.     {
    19.         rb.isKinematic = true;
    20.         rb.detectCollisions = false;
    21.         gameObject.transform.SetParent(PizzaPub.gameObject.transform);
    22.     }
    24.     [ClientRpc]
    25.     public void AssingParentClientRpc()
    26.     {
    27.         rb.isKinematic = true;
    28.         rb.detectCollisions = false;
    29.         gameObject.transform.SetParent(PizzaPub.gameObject.transform);
    30.     }
    Any suggestion is hapily recieved. Sorry for my bad spelling, idk a lot about english :(
  2. strangirs


    Mar 27, 2022
    Hi. Try removing the Client RPC method and only call AssignParentServerRpc. Also make sure that the server owns the game object. Also, does the object has a network transform? You will also have to make sure the server moves the object and not the client. Let me know if this helps at all..