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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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:
    4,360
    Carl458 likes this.