Is it possible to make GameObjects Serializable in ServerRpc?

I am constantly running into an issue where I wish to spawn a GameObject but it has to be done using ServerRpc. If I am unable to put in the gameobject to be spawned in the ServerRpc, then I would need to have the Server Side keeping a list of all the GameObject Prefabs to spawn it.

Is there a way to make GameObjects Serializable in NGO?

Exactly. There is this prefabs list in the NetworkManager that is for this exact purpose.

Serializing GameObjects via RPC would be a tremendous waste of a precious resource: bandwidth. And it cannot be done anyway because consider all the things it would need to serialize, mainly all of its components. Needlessly so.

Instead, you instruct the server via ServerRPC to spawn "prefab number 3457" and it will take that as an index into the NetworkManager's prefab list, or your own.

To make this easier for you, you can encode the indexes as an enum and write a little editor script that allows a prefab to add itself to a prefab list using its own enum index (and warn if another prefab registered itself using the same index to let you know about and fix such collisions). That way you no longer have to think about prefab registration anymore, you'd just assign a prefab a value from the enum once and be done with it.

1 Like

I see. Thanks for the reply.

Then how would you manage upgrades to the list of Prefabs on the NetworkManager in this case? For example, if Player 1 upgrades prefab number 3457 damage +10% but player 2 doesn't have the upgrade. Throughout the game, multuple upgrades will be done. Do I have to create a separate GameManager script that tracks all the prefabs and their stats for every single player? Or is there a smarter way to go about doing this?

This could be encoded in the same prefab. For increased damage, you could enable/disable components or just send the new damagePercentBonus value that's applied all the time anyway and defaults to 1.0f.

For visual elements, like shiny armor rather than glossy armor, you can either change material or meshrenderer properties or enable/disable game objects like particle fx. Alternatively, since the "shiny armor" mesh is in all builds, you could simply instruct everyone to load a different mesh into the meshfilter.

ScriptableObjects are great for this because they can have lists of assets (like meshes) and you only need to send an index for the asset that should be loaded.

1 Like