Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Gameobject for ServerRpc parameter

Discussion in 'Netcode for GameObjects' started by SuhoShin, Jul 23, 2023.

  1. SuhoShin

    SuhoShin

    Joined:
    Mar 18, 2023
    Posts:
    1
    if I use Gameobject for ServerRpc parameter, there are an error :
    "Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP: Assets\Scripts\ThirdPersonController.cs(555,9): error - CreateEffectServerRpc - Don't know how to serialize UnityEngine.GameObject. RPC parameter types must either implement INetworkSerializeByMemcpy or INetworkSerializable. If this type is external and you are sure its memory layout makes it serializable by memcpy, you can replace UnityEngine.GameObject with ForceNetworkSerializeByMemcpy`1<UnityEngine.GameObject>, or you can create extension methods for FastBufferReader.ReadValueSafe(this FastBufferReader, out UnityEngine.GameObject) and FastBufferWriter.WriteValueSafe(this FastBufferWriter, in UnityEngine.GameObject) to define serialization for this type."

    and how do you use Gameobject for ServerRpc parameter?
    Code (CSharp):
    1. [ServerRpc]
    2.     private void CreateEffectServerRpc(GameObject prefab, Vector3 position, Quaternion rotation, float destroyTime)
    3.     {
    4.         GameObject effect = Instantiate(prefab, position, rotation);
    5.         effect.GetComponent<NetworkObject>().Spawn(true);
    6.         StartCoroutine(DestroyEffect(effect, destroyTime));
    7.     }
     
  2. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    53
    Hi,

    The answer is : you don't.
    Why would you want to send a GameObject through the network ?
    In your case, you want to send a prefab to instantiate it. If you want something to let the player dictates which prefab the server has to spawn, the player has to send this information another way.

    In your case : I imagine that you have multiple prefabs that could be spawned to create an "effect". As a server, you could store all the prefabs of an "effect", and make the player choose : could be by an index, or something like a name.
    If you don't have much variants, you could just call "CreateXEffectServerRpc", and the server already knows which effect it has to spawn.

    Hope this helps !
     
    Last edited: Jul 24, 2023