Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved How do I spawn a Network Object on all other clients apart from the sender

Discussion in 'Multiplayer' started by Carl458, Nov 8, 2023.

  1. Carl458

    Carl458

    Joined:
    Apr 1, 2014
    Posts:
    1
    How do I spawn a Network Object on all other clients apart from the sender
    I have a fireball spell that I want to Instantiate on local client A, and then send a ServerRpc request to spawn the object on the Network for all other clients but not for client A
    The instantiated version is instant so there is no lag in casting or seeing the fireball move for the local client.
    But then when the server spawns the network version it also spawns again for Client A so they see 2 fireballs.

    I have tried using a ulong array of all clientIDs apart from the LocalClientId and feeding it into ClientRpcParams etc but I can't seem to get anything to work. The Network Object still spawns for Client A

    Here is my base code with the Params taken out as it wasn't working:


    Code (CSharp):
    1.  
    2. using Unity.Netcode;
    3.  
    4. private IEnumerator CastingFireballs()
    5.     {
    6.         while (attacking && IsOwner)
    7.         {
    8.             GetPlayerDirection();
    9.             CastFireballLocal();
    10.             CastFireballServerRPC(playerDirection);
    11.             yield return new WaitForSeconds(0.7f);
    12.         }
    13.         yield return null;
    14.  
    15.     }
    16.  
    17.     private void CastFireballLocal()
    18.     {
    19.         if (!IsLocalPlayer) return;
    20.         GameObject fireball = Instantiate(spells[0], castPoint.position, transform.rotation);
    21.         fireball.GetComponent<Fireball>().speed = 10f;
    22.         fireball.GetComponent<Fireball>().direction = playerDirection;
    23.     }
    24.  
    25. [ServerRpc]
    26.     public void CastFireballServerRPC(Vector3 direction)
    27.     {
    28.         if (IsServer)
    29.         {
    30.             CastFireballClientRPC(direction);
    31.         }
    32.     }
    33.  
    34.     [ClientRpc]
    35.     public void CastFireballClientRPC(Vector3 direction)
    36.     {
    37.         if (!IsServer) return;
    38.         GameObject fireball = Instantiate(spells[0], castPoint.position, transform.rotation);
    39.         fireball.GetComponent<Fireball>().speed = 10f;
    40.         fireball.GetComponent<Fireball>().direction = direction;
    41.         fireball.GetComponent<NetworkObject>().Spawn();
    42.  
    43.     }
     
    Last edited: Nov 10, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    Carl458 likes this.