prefab is null after connecting to Server

Hello, I’m currently making multiplay shooting game using unity net code.
current version is unity 2022.3.20f1, and net code 1.8.1.

what I did is

  1. create empty object called Network Manager and attach Network Manager and Unity Transport component.
  2. assign player prefab, and create network prefabs list which has player prefab and bullet prefab, and assign it into network manager component.
  3. In player prefab and bullet prefab, add Network Object and Network Transform component.
  4. create empty object Called Shooting Manager and add Network Object and Shooting Controller Script.

this is Shooting Controller script.

using UnityEngine;
using Unity.Netcode;

public class ShootingController : NetworkBehaviour
{
    public GameObject bulletPrefab; // Assign your bullet prefab in the Inspector

    void Update()
    {
        // Detect user input (left click or touch)
        // Ensure the input is processed only if this is the owner's client
        if (IsClient && IsOwner && Input.GetMouseButtonDown(0))
        {
            ShootBulletServerRpc(NetworkManager.Singleton.LocalClientId);
        }
    }

    [ServerRpc(RequireOwnership = true)]
    private void ShootBulletServerRpc(ulong clientId)
    {
        // Instantiate the bullet at the camera's position and rotation
        GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
        NetworkObject networkObject = bullet.GetComponent<NetworkObject>();
        if (networkObject != null)
        {
            networkObject.Spawn(); // Spawn the bullet over the network

            // Initialize the bullet's properties, such as setting its owner
            BulletController bulletController = bullet.GetComponent<BulletController>();
            if (bulletController != null)
            {
                bulletController.SetOwner(clientId);
            }
        }
    }
}

and then, I assigned bullet prefab in the Shooting Manager.
After that, I build it for linux dedicated server , and upload the build to the server and run.
it’s kinda working, but I’m having null reference error in GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation); even though prefab has been assigned.
so I put log and found, after connecting to the server, suddenly bulletPrefab becomes null.
I do not have any clue why this is happening.

ShootingController is on an object that’s placed in the scene?
If not, it needs to be spawned as well.

Testing with builds is awful. You want to be using ParrelSync or go straight to Unity 6 beta and use MPPM (Multiplayer PlayMode) which is such a wonderful thing to test networking with, you’ll be easily ten times as productive than making builds every time.

The SetOwner call is a Netcode method? If so, you can use SpawnWithOwnership to simplify this code. You don’t need the BulletController for setting ownership, SetOwner works on the NetworkObject just as well.

Thanks for the reply.
ShootingController is on an object in the scene. and the bullet prefab is attached to the script, but not on the scene. so I’m trying to spawn it but keep getting null reference error…

oh, ParrelSync looks really good! thanks for the advice! I’ll test with it!!

I believe SetOwner call is a Netcode method. so, I changed to SpawnWithOwnership, and it works fine! thank you!

so, current issue is null reference error in prefab.
After connecting to the server, suddenly prefab becomes null :frowning:
I don’t know what causes it…