Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Resolved Netcode: GameObject as parameter of ServerRpc Function

Discussion in 'Netcode for GameObjects' started by predows, Nov 18, 2022.

  1. predows

    predows

    Joined:
    Apr 25, 2021
    Posts:
    2
    I'm working on a multiplayer game using Netcode for GameObjects. I need to change the ownership of the car to let clients drive. This is my function:

    Code (CSharp):
    1. [ServerRpc]
    2. private void EnterCarServerRpc(GameObject net)
    3. {
    4.         Debug.Log("Start");
    5.         isDriving = true;
    6.         net.GetComponent<carController>().havePilot = true;
    7.         net.GetComponent<NetworkObject>().ChangeOwnership(OwnerClientId);
    8.         Debug.Log("SERVER: "+OwnerClientId+" is driving the car.");
    9.            
    10. }
    But the console logs this error:
    I didn't found how to do this in the documentation, so how can i fix this?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,629
    In the very least, post on the Netcode for GameObjects forum. I'll move your post for you.
     
    lavagoatGG likes this.
  3. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    198
    Since your GameObject has a NetworkObject attached, pass it as a NetworkObjectReference instead.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    402
    Something like this should work:
    Code (CSharp):
    1.     [ServerRpc]
    2.     private void EnterCarServerRpc(NetworkBehaviourReference carReference, ServerRpcParams serverRpcParams)
    3.     {
    4.         ulong clientId = serverRpcParams.Receive.SenderClientId;
    5.  
    6.         if (carReference.TryGet<Car>(out Car car))
    7.         {
    8.             Debug.Log("Start");
    9.             isDriving = true;
    10.             car.GetComponent<carController>().havePilot = true;
    11.             car.NetworkObject.ChangeOwnership(clientId);
    12.             Debug.Log("SERVER: " + clientId + " is driving the car.");
    13.         }
    14.         else
    15.         {
    16.             Debug.LogError("Didn't get car");
    17.         }
    18.     }
    Call it with:
    Code (CSharp):
    1.    player.EnterCarServerRpc(car, default);
     
    predows and lavagoatGG like this.
  5. predows

    predows

    Joined:
    Apr 25, 2021
    Posts:
    2
    Thank you! Tried here and works fine :D
     
    RikuTheFuffs-U and cerestorm like this.