Still new to Netcode for GameObjects but I am a bit stuck on what seems like a bug. My game works great in the editor and my network prefab list contains all of the necessary prefabs I need to spawn. However, when I make a build and try to spawn one of the network prefabs I get the error:
The NetworkObject parameter is not a registered network prefab. Did you forget to register it or are you trying to instantiate and spawn an instance of a network prefab?
I don’t understand what I have done wrong here. My network prefab list contains it in the editor and works perfectly there. All I am doing is storing a reference to the NetworkObject prefab from my project folder in a MonoBehaviour and calling:
[SerializeField] private NetworkObject m_MyPrefab;
private void Start()
{
if (!NetworkManager.Singleton.IsServer)
return;
NetworkObject myObject = NetworkManager.Singleton.SpawnManager.InstantiateAndSpawn(m_MyPrefab, destroyWithScene: true);
// Do stuff with myObject...
}
Has anyone else run into a similar issue? I am using Unity version 6000.0.19f1 and Netcode for GameObjects version 2.0.0-pre.4 by the way.
You can’t spawn in Start. All Netcode should be handled in OnNetworkSpawn or any event that runs later than that (depends on whether object is spawned or in-scene placed).
Check what you have assigned to m_MyPrefab. It may not actually be a prefab (asset) reference but a reference to an in-scene object. But I suppose the problem is due to the above.
Thanks for the reply! I tried to look at those two things.
For the prefab asset reference I double checked that it is definitely an asset and not a scene object.
Then for the Start() spawning of the network prefab I wasn’t quite sure that it being in start is the issue. I know what you mean with having spawning happen in OnNetworkSpawn() but the script I have this in isn’t a NetworkBehaviour since it didn’t really need to be (hence why I use the NetworkManager.Singleton). The reason it is safe to have my prefab spawning in Start is because the object that holds this script was also a prefab that was spawned in (its a tower defense, and the tower that was spawned is now spawning projectiles).
Anyways, I don’t know why this fixes anything, but I ended up changing the InstantiateAndSpawn() method to separately Instantiate() and then Spawn() like this: