Question Details

No question body available.

Tags

c# arrays unity-game-engine netcode

Answers (2)

March 17, 2026 Score: 2 Rep: 93,166 Quality: Medium Completeness: 100%

See Arrays and native containers

You can basically go similar to the Job System and make use of Unity's struct based NativeArray for this.

Alternatively as mentioned on the page above as well you can also simply implement your own array like struct that implements INetworkSerializable or even do it in your wrapper type directly which anyway already implements the interface itself.

For your array it would basically be something like

  • serialize length
  • then serialize flat out all elements

and for receiving wise versa

  • deserialize length
  • create empty array with according length
  • loop and deserialize the items

In code that might look somewhat like e.g.

int length = array != null ? array.Length : 0;

serializer.SerializeValue(ref length);

if (serializer.IsReader) { array = new SimpleDataStruct[length]; }

for (int i = 0; i < length; i++) { serializer.SerializeValue(ref array[i]); }

My guess would be that NativeArray basically does pretty much something like this anyway - it's just a bit more of a hassle regarding allocation and lifetime management of those.

Since you say the size is dynamically changing all the time anyway (army units) it would probably make most sense to go for a List type(s) in general.

March 17, 2026 Score: 2 Rep: 89 Quality: Low Completeness: 60%

While not directly answering the original question, I ended up finding a workaround that actually works great and is way simpler than my initial attempt.

Instead of having a 3-step hierarchy (NetworkBehaviour -> ContainerStruct -> SimpleDataStruct), I simply merged the first 2. Here's the new structure :

public class PlayerArmy : NetworkBehaviour
{
    private NetworkList dataList = new(new List(), NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
    private NetworkVariable moreData = new(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
    ... More NetworkVariables ...
}

Since SimpleDataStruct is a value type and only contains non-nullable value types, I can use the NetworkList , but the other variables are just NetworkVariable , all on a NetworkBehaviour Game Object that I spawn in when needed.

This is so much simpler to implement than having an in-between class since all the values are automatically synchronised perfectly. I don't know why I didn't think of this first...