4
\$\begingroup\$

I am trying to figure out how to dynamically add network components to game objects that are managed by the network.

I started unity about a month ago, and I started writing all sorts of components that I then wired together in the editor. I have now reached the point where I realize how cumbersome it is to manage a complex project that is built that way.

I am now going through and trying to create "controller" mono behaviors for all of my entities. The intent is that the controller will use AddComponent to add the behaviors it needs. So instead of creating a prefab with a dozen components that are all wired together in the editor, I create a prefab with a single controller component. That controller then adds components like a rigid body, collider, etc., and wires them together via code.

This works well until I get to networking the entity. It seems like network components need to be part of the prefab or they don't work at all. I tried using OnStartServer and OnStartClient to add NetworkTransform components, but the transform wasn't synchronizing. I could add the NetworkTransform to the prefab, but then it needs a rigid body... and I suspect other network behaviors would also need to be there. And then I'm right back where I started, with a large, opaque prefab.

I have no problems getting networked entities working with monolithic prefabs and the built in network manager. But when I try the " grown up" way, by adding components in code, it goes pear - shaped.

Is what I'm attempting even possible? How deep do I have to go into unity's networking in order to do this? Is there a way that involves the sparsely documented ClientScene methods? Do I need to somehow make the network server aware of the newly added NetworkTransform?

\$\endgroup\$
2
\$\begingroup\$

Maybe it is possible, but I don't think that this is supported in the new HLAPI network system. What you could do is have all the componentes in the prefab you instantiate disabled, and enabling them during runtime

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.