Synchronize Scripts between Server and Client

So I do have a basic understanding of the networking concepts of Unity.

I however get a little bit confused of what gets replicated. My Understanding is:

  • Everything that is a NetworkBehaviour, is capable of snychronizing data cross server/client
  • NetworkVariables/NetworkLists are the go-to way to synchronize data between server/client

Now I am building a game where the server is spawning units (which in return have abilities) and I want those to be available at the client too. For that I would apply a NetworkObject to the gameobject, add my needed scripts (NetworkBehaviour for those that “share” data, Monobehaviour for those that dont). One of those NetworkBehaviours is “Unit”.

So that the player can keep track of the cooldown of abilities or the health of the unit, all those variables would need to be NetworkVariables. The place where I struggle now tho is:
How do I replicate the List of Units from the Server to the Client? I tried NetworkList, but apparently I can only use structs here. Should I just replicate the NetworkIds and then get the Unit from their replicated NetworkObjects? Or should I implement this ISerializable interface, which looked very complicated with alle the values (animations, abilities, …) a unit has.

Help/Insight would be highly appreciated. Maybe I also just misunderstood some concepts.

Replicate the list on the client side by hooking into OnNetworkSpawn, adding each Unit to the client‘s list when the units’ owner matches the local client.

1 Like

This works thanks. As I am a bit confused: Why does this sync between server/client if it is not a NetworkVariable? Isnt this the whole purpose of a NetworkVariable?

A lot of newcomers to networking seem to be focused on actually transmitting (synchronizing) everything that needs to be in sync. However the reality is that you can often replicate the same behaviour on the client side with the information available to them (this case, every spawned object runs OnNetworkSpawn and the state can be replicated from that).

Or where there is no synchronization necessary, for example if one player starts shooting a machine gun weapon. Commonly one might be tempted to send an RPC for every bullet fired and also making each bullet a network spawned object. However, you only need two RPC calls: start and stop shooting weapon. What kind of weapon is shooting is however already known to the client, thus the client can infer the frequency with which it spawns projectiles from that. On top the behaviour of the projectiles (eg speed, visuals) is known too and collision handled by the server/host, so the client just does local collision handling but only performs visualization of bullet impacts. This leads to the odd visually hitting or missing bullet that actually missed or hit - but this is normal behaviour in multiplayer games.

1 Like