Search Unity

Resolved Passing GameObject to ServerRPC

Discussion in 'Netcode for GameObjects' started by unity_VngtZy77FXMDEg, May 12, 2023.

  1. unity_VngtZy77FXMDEg

    unity_VngtZy77FXMDEg

    Joined:
    Sep 1, 2021
    Posts:
    5
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    If the serverRpc is on the hit object you can just pass in the values as parameters. You'll have to elaborate on how your objects are set up.

    I know nothing about colliders or rigidbodies and such so I may not be able to give you the correct solution for your problem. :)
     
  3. unity_VngtZy77FXMDEg

    unity_VngtZy77FXMDEg

    Joined:
    Sep 1, 2021
    Posts:
    5
    Hi thanks for the rply. I have the OnTriggerEnter and ServerRPC script inside a bullet prefab and i want to call a serverrpc with the hit object as a parameter inside the OnTriggerEnter method when the bullet hits a player.
    like this:
    private void OnTriggerEnter(Collider other)
    {
    if(other.CompareTag("Player"))
    {
    TakeDMGServerRPC(other.gameObject)
    }
    }
    [ServerRpc]
    public void TakeDMGServerRPC(GameObject hitPlayer)
    {
    hitPlayer.GetComponent<Stats>().currentHealth.Value -= 20;
    }
    I have fixed the issue for now with this
    private void OnTriggerEnter(Collider other)
    {
    if(!IsServer) return;
    if(other.CompareTag("Player"))
    {
    TakeDMGServerRPC(other.gameObject)

    }
    }
    but I would like to now a method to give a gameobject as parameter in serverrpc
    I think like this post https://forum.unity.com/threads/netcode-gameobject-as-parameter-of-serverrpc-function.1363132/
    but I didnt understand this xD (sorry for my bad english)
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    This may work but I've not tried it:
    Code (CSharp):
    1. [ServerRpc]
    2. public void TakeDMGServerRPC(NetworkObjectReference playerGameObject)
    3. {
    4. playerGameObject.GetComponent<Stats>().currentHealth.Value -= 20;
    5. }
    NetworkBehaviourReference is another option as the rpc parameter where you can pass in a NetworkBehaviour like Player or Stats in directly rather than the game object.
     
  5. unity_VngtZy77FXMDEg

    unity_VngtZy77FXMDEg

    Joined:
    Sep 1, 2021
    Posts:
    5
    Thank you :) I added if(!playerGameObject.TryGet(out NetworkObject networkObject) and now it works

    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other)
    2.     {
    3.         if(other.CompareTag("Player"))
    4.         {        
    5.             TakeDMGServerRPC(other.gameObject.transform.parent.gameObject);
    6.         }
    7.     }
    8.     [ServerRpc]
    9.     public void TakeDMGServerRPC(NetworkObjectReference playerGameObject)
    10.     {
    11.         if (!IsOwner) return;
    12.         if(!playerGameObject.TryGet(out NetworkObject networkObject))
    13.         {
    14.             Debug.Log("error");
    15.         }
    16.         networkObject.transform.GetChild(1).gameObject.GetComponent<Stats>().currentHealth.Value -= 20;
    17.  
    18.     }
    19. }
     
    MrMammothWings and cerestorm like this.