# How to correctly sync an ArrayList of GameObjects on a server/client

I'm experimenting with networking in Unity and I can't seem to get an ArrayList of GameObjects to sync from the server to the client.

Here is the relevant code:

[SyncVar]
ArrayList walls = new ArrayList();

void Start ()
{
grid = new bool[width, height];

if(isServer)
{
// This is an algorithm to generate a Maze.
// It adds the walls of the maze (GameObjects)
// to the walls array as the algorithm runs.
CarvePassagesFrom(0, 0, south, grid);
}
// If this instance of the game isn't the server, we want to get the
// already generated maze from the walls ArrayList (that should have synced)
else
{
foreach (GameObject go in walls)
{
Debug.Log("Creating game object: " + go);
Instantiate(go);
}
}
}


I have the console tell me how many walls were added in total after the algorithm has run and the walls have definitely been added. But on the client the size of the walls array is 0.

Am I missing something really obvious about the syncing of variables?

The [SyncVar] attribute only works on simple data types. You can't synchronize generic objects, or lists/arrays of generic objects. SyncListStruct will allow you to synchronize lists of structs, however. Probably the closest thing you can get to what you want (without writing your on OnSerialize and OnDeserialize methods) is something like this:

public struct Wall {
// define wall members here
}

public class SyncListWall : SyncListStruct<Wall> {}

public class MyScript : NetworkBehaviour {

public SyncListWall walls = new SyncListWall();

}


You'll then have build your local ArrayList from the data in the SyncListStruct.

Note that you don't need any attributes as with [SyncVar]. Also there are a couple things that will get you if you're not careful:

• You need to create a class that inherits from SyncListStruct - you can't just use it directly.
• The class declaration has to be above where the synclist gets used in the class file. If it's after (eg below the NetworkBehaviour) you won't get any errors or warnings, but the list contents won't propagate over the network.

More info on SyncListStruct. In particular, look at SyncListChanged, which you'll need to populate to handle changes to the list after initialization.