So I have a OnTriggerEnter function which should execute a serverRPC and change some values on the HIT object. I have read this post Netcode: GameObject as parameter of ServerRpc Function but couldnt understand how it works. Can someone help please?
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.
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().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://discussions.unity.com/t/900556
but I didnt understand this xD (sorry for my bad english)
This may work but I’ve not tried it:
[ServerRpc]
public void TakeDMGServerRPC(NetworkObjectReference playerGameObject)
{
playerGameObject.GetComponent<Stats>().currentHealth.Value -= 20;
}
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.
Thank you I added if(!playerGameObject.TryGet(out NetworkObject networkObject) and now it works
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
TakeDMGServerRPC(other.gameObject.transform.parent.gameObject);
}
}
[ServerRpc]
public void TakeDMGServerRPC(NetworkObjectReference playerGameObject)
{
if (!IsOwner) return;
if(!playerGameObject.TryGet(out NetworkObject networkObject))
{
Debug.Log("error");
}
networkObject.transform.GetChild(1).gameObject.GetComponent<Stats>().currentHealth.Value -= 20;
}
}