SyncList NullReferenceException

I have a SyncListFloat in a main script, and I am attempting to have a reference to that SyncList in a second script that is assigned when the two scripts are linked. However, if I do not initialize the SyncList with “new” I get an error at runtime… but I don’t want two different “new” separate SyncLists.

This is the error I get.
NullReferenceException: Object reference not set to an instance of an object
TestSyncListHandle…ctor ()

using UnityEngine;
using System.Collections;
using UnityEngine.Networking; //needed for NetworkBehavior

public class TestSyncListHandle : NetworkBehaviour 
{
    private SyncListFloat syncListFloatOne = new SyncListFloat(); //this works
    private SyncListFloat syncListFloatTwo; //this causes error
}

Anybody else having trouble with this? Thanks in advance.

I experience the same behaviour and found your thread on searching here myself.

If I initialise the SyncList in my public field I don’t get the error, but if I try initialise the SyncList anywhere else I get the same error referring to the class constructor.

I can’t find much documentation or help on using SyncLists - the official documentation on them is very thin and not very explanatory.

I would like to know how to use them perhaps via some simple examples. Some questions I have:

  • Do we need to add the SyncVar attribute on them? I assume not seeing as though I was able to see the content of one from the other client
  • How to initialise them
  • When initialising them, don’t they work just like normal IEnumerable Lists? i.e. surely they clear out if we ‘new’ them up - e.g. SyncListString mysyncList = new SyncListString(); - I would expect that to overwrite the SyncList and sync across clients (i.e. clear it out for everyone, which is not what I want).