Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved parenting not working for client

Discussion in 'Netcode for GameObjects' started by liambilly, Feb 26, 2023.

  1. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i have this code in player script its not working on client side for parenting a networkobject
    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.     void ChangeOwnerNParServerRpc()
    3.     {
    4.         vw.netObj.ChangeOwnership(OwnerClientId);
    5.         //vw.netObj.TrySetParent(transform);
    6.         vw.transform.parent = transform;
    7.     }
    8. void OnTriggerEnter(Collider other)
    9.     {
    10. if(!IsOwner)return;
    11.         if (other.transform.TryGetComponent<VisualWeapon>(out vw))//if it has this component
    12.         {
    13.             if (!vw.realWeapon1)//if null
    14.             {
    15.                 foreach (var gun in realWeapons)
    16.                 {
    17.                     if (gun.tag == vw.tag)
    18.                     {
    19.                         vw.realWeapon1 = gun;
    20.                         SetWeaponSlot(gun.transform, gun.weaponData);
    21.                         break;
    22.                     }
    23.                 }
    24.                 vw.gameObject.layer = gameObject.layer;
    25.              
    26. ChangeOwnerNParServerRpc();
    27.             }
    28.         }
    29.        
    30.     }
    31.  
     
    Last edited: Feb 27, 2023
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    The serverRPC executes on the server so it would parent the server's transform I think.
     
  3. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i looked at the clientdriven example code,the parenting logic is under a serverrpc, but what ur saying makes sense too letme try
     
  4. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i just tried it
    i used a clientrpc withserverpc and got an exception
    NotServerException: Only the server can reparent NetworkObjects
    UnityEngine.Debug:LogException(Exception)
     
    Last edited: Feb 27, 2023
  5. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    solved it by removing the check for
    isowner