[Netcode] How to add a Network Behaviour component to a Network Object?

I need to add a component (derived from a NetworkBehaviour class) to a Network Prefab. I tried instantiating the prefab, adding the component and then spawning the prefab. As a result I see the prefab being spawned across the network but without the component and a with an error "Network Behaviour index 256 was out of bounds for (Network Object that spawned)" on the client side. On the server side everything works as intended (haven't tested whether the component works as a NetworkBehaviour component yet).

Put the NetworkBehaviour on that prefab in the editor, then instantiate. You cannot add NetworkBehaviour components at runtime. If you need code that‘s different based on player etc. that behaviour has to be coded into the component.


I am having hard time understanding the terminology here. a NetworkObject is literally a component that you can add to any game objects. NetworkBehaviour is class inherited from MonoBehaviour. So what do they actually mean when they say add NetworkBehaviour component to an object? it would be appreciate it if you can elaborate more on this.

NetworkObject handles the networked state of the entire object. There must be only one per network prefab.

NetworkBehaviour is a network-enabled MonoBehaviour. You can have several on a prefab, each handling individual networked aspects of the prefab like movement, shooting, inventory and what not. They can use NetworkVariables and register to receive network events. NetworkTransform is one of those specialized NetworkBehaviour components.

Sorry to bring back such an old issue. I am making a game where I instantiate the avatar weapons after the avatar has been spawned. To avoid nesting several network objects in the same hierarchy (the weapon being a child of the avatar's hand) I made the weapon simply cosmetic, and have a system to have it instantiated (locally) in every client. That works fine, and the wepon looks good. Also, I add the spawn point transform for the projectile in every client, and all those weapon-related stuff are monobehaviour.
So, now I must add a component derived from network behaviour in the avatar, which will manage to spawn the network object of a projectile (only in the server). And it stopped working.

Now, what would the internet advise me to do?
1- Is there a way to add such network behaviours at runtime, after so much updates?
2- If not, can I have multiple game objects in the hierarchy without problems now?
3- Or should I go to some other approach, such as FixedJoints to simulate parenting and have NetworkBehaviours on both Avatar and Wepon in defferent hierarchies?

Thanks in advance.
Btw, I did not worte any code because my project is rather complex by now, and narrowing down to the problem would be more troublesome than explaining the idead behind it.


Afaik no. Networked components have to be in the scene or on the prefab already.

The solution is simple though: simply enable/disable the component as needed.


Unclear what you mean by this. Nested network prefabs are possible but they need to be spawned and reparented via network methods.

The way I would solve this is to put all the network components on the Avatar. You'd have a NetworkWeapon component on the root. It doesn't care what weapon you're currently holding, and any weapon component would simply implement a common IWeaponComponent interface.

The NetworkWeapon component can either provide events for the actual weapon to hook into, or it can get a reference to the currently held weapon and call methods on it.

If you have a fixed number of weapons you can even put them in the player's hands, and merely enable just the one currently selected, to avoid needing separate weapon prefabs.

There's no necessity to have a networked thing actually have a NetworkBehaviour on it. If you wanted to, you could put all your network code in a single prefab on each network prefabs, and it will merely orchestrate what is supposed to be done to other components further down the hierarchy (or invoke events where it's more meaningful for child components to search upwards in the hierarchy).