Search Unity

Is it possible to allow client to spawn network object ?

Discussion in 'Netcode for GameObjects' started by Hytrozion, Oct 1, 2022.

  1. Hytrozion

    Hytrozion

    Joined:
    Aug 12, 2021
    Posts:
    4
    Hi guys,

    I'm running into some issue with netcode. I'm making an multiplayer fps and bullets are rigidbody.

    when the host shoot, it's working fine, no issue what so ever, but the client have this error when trying to spawn a network object:

    NotServerException: Only server can spawn NetworkObjects


    here is the piece of code
    Code (CSharp):
    1.     public void Shoot(Vector3 raycastHit)
    2.     {
    3.         bulletSpawnPoint.LookAt(raycastHit, bulletSpawnPoint.forward);
    4.         Transform bullet = Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation);
    5.         bullet.GetComponent<Rigidbody>().velocity = bulletSpawnPoint.forward * bulletSpeed;
    6.         bullet.GetComponent<NetworkObject>().Spawn(true); //error is at this line
    7.     }
    is there a way to allow client to spawn network objects in the overall project ? or do I have to make a serverRPC ?
    (I'm trusting clients since this will be a game to play with friends)

    thanks you !
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Yes, you will have to make a ServerRpc to do the spawning.
     
    boffer likes this.
  3. Hytrozion

    Hytrozion

    Joined:
    Aug 12, 2021
    Posts:
    4
    yeah that what I did, thanks you.
     
  4. Moon12345

    Moon12345

    Joined:
    Feb 6, 2019
    Posts:
    1
    for some reason when a client spawns an object using serverrpc i still get the error "NotServerException: Only server can spawn NetworkObjects". Any ideas why?

    ```

    [ServerRpc]
    private void InitCraftedPrefabServerRpc(string craftPrefab, Vector3 position, Quaternion rotation, bool enableClip)
    {
    GameObject prefab = Instantiate(Resources.Load(craftPrefab)) as GameObject;
    prefab.transform.position = position;
    prefab.transform.rotation = rotation;
    prefab.GetComponent<NetworkObject>().Spawn();
    }
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Are you sure the exception originates from that piece of code? At a first glance, everthing there seems fine
     
  6. MafiaMoe

    MafiaMoe

    Joined:
    Mar 24, 2013
    Posts:
    27
    Does the ServerRpc need ServerRpcParams defined to be utilized correctly and run on the server?

    eg.
    private void InitCraftedPrefabServerRpc(string craftPrefab, Vector3 position, Quaternion rotation, bool enableClip, ServerRpcParams rpcParams = default)
     
  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Nope
     
  8. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Is your class deriving from NetworkBehaviour?