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 Reparent player to networkObject

Discussion in 'Netcode for GameObjects' started by Korthking, Feb 1, 2022.

  1. Korthking

    Korthking

    Joined:
    Jan 12, 2021
    Posts:
    5
    I am trying to reparent the player into a networkObject's child (a car seat for instance).

    I am doing this by raytracing from the player to the vehicle, and activate a "take control" script on the vehicle, which turns on and off stuff.

    Now in order to actually do the reparenting I need to do this on the server, so in the vehicle's "take control" script I activate a ServerRpc to handle the reparenting with the client's local networkId.

    I get an Error saying only the owner can send an ServerRpc with reparenting. What is happening here? I tried to move the reparenting ServerRpc and call to the method into the player's "interact" script, but the same issue comes up.

    I don't have any of the code on this machine, but hopefully this is enough to get a general guide and workflow on how to approach this. If not I'll update this with the relevant code.

    Some notes:
    At this moment, the networkObject I'm trying to reparent TO is already in the scene from start, and not spawned in after server start. I don't know if this can cause an issue regarding networkObjectId or spawnedObjectsList?
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    Might need to see the code and the full error, but it could just be a case of needing to use

    [ServerRpc(RequireOwnership = false)]


    if you don't have ownership on the object you're calling from.
     
    Korthking likes this.
  3. Korthking

    Korthking

    Joined:
    Jan 12, 2021
    Posts:
    5
    Thank you for the quick response.

    I'll post the error and relevant code when I get home from work.

    I contemplated doing that, but avoided it due to believing it's bad practice. I may try this when I get home.

    Bonus question: I see that someone reparents by calling ServerRpc, which gets the networkObject's networkId, then trigger the reparenting on ClientRpc. I haven't tried this, but wouldn't that also throw the error for only server being able to reparent?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    I would have thought so but I've not tried it, I've only ever handled parenting on the server on object spawn.
     
    Korthking likes this.
  5. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Perhaps they were reparenting to the root (i.e. to null)? That is as far as I know the only reparenting that a client can do itself.
     
    Korthking likes this.
  6. Korthking

    Korthking

    Joined:
    Jan 12, 2021
    Posts:
    5
    After trying around more, I decided to go with the tip of just disabeling ownership requirement for the ServerRpc
     
  7. Nocazone

    Nocazone

    Joined:
    Dec 7, 2021
    Posts:
    20
    Hey, I had that problem too. I didn't disable ownership but instead of reparent a NetworkObject, I am doing this:
    Scene
    - I have a NetworkObject with a list of custom Prefabs that need to be instantiated (not the NetworkManager list)
    - I have a NetworkTransform LootBubble (Registered in the NW Manager)
    - The player is a NetworkObject and registered in the NW Manager (the player has bone object where I need to place the weapon)
    - The weapon registered has a number of the position in the custom PrefabList
    Runtime:
    - At collision with the Lootbubble i take read the position in the PrefabList from the weapon
    - On The server I instantiate the weapon and add it to the place in the PlayerPrefab as a child (ServerRPC)
    - Then i do the same on all Clients (Client RPC)

    Maybe that helps you too