#allow client to spawn network object

1 messages · Page 1 of 1 (latest)

edgy shuttle
#

Hi guys, I'm making a fps multiplayer game, and bullets are not raycasted but rather spawned with rigid body and velocity.
I have an issue with this, the host can shoot no problem, but client cannot and the console give me the following error
NotServerException: Only server can spawn NetworkObjects

is there a solution to allow client to spawn network objects ? or do I have to make a serverRPC ?

here is the code that spawn the bullet :

public void Shoot(Vector3 raycastHit)
    {
        audioSource.PlayOneShot(sound);
        muzzleflash.GetComponent<ParticleSystem>().Play();
        bulletSpawnPoint.LookAt(raycastHit, bulletSpawnPoint.forward);
        Transform bullet = Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bulletSpawnPoint.forward * bulletSpeed;
        bullet.GetComponent<NetworkObject>().Spawn(true);
    }

thanks you !