SyncLists are initialized, but I get Error message: "SyncList not initialized."

I’m just trying to get all float values from 5 different list of floats from client to the server, and server to the client. I need these 5 lists of floats, all created from user input, so that I can be able to manipulate them.

However, I’m getting all sorts of weird errors.

This is the error message I am getting:

The initialization code is there. Initialize() runs fine.

And this is the most interesting bit:

healthPrefabSyncList is not null.

Yet, it reports an error saying the SyncList isn’t initialized.

I’m confused by this.

Should I report a bug for this? I feel the error message is misleading me, or it’s something else that I’m misled to believe it’s not initialized.

Any suggestions?

Full code is given below:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections.Generic;
using System;

public class UnitAttributes : NetworkBehaviour {
    public List<float> healthPrefabList;
    public List<float> attackPrefabList;
    public List<float> speedPrefabList;
    public List<float> mergePrefabList;
    public List<float> splitPrefabList;

    public SyncListFloat healthPrefabSyncList;
    public SyncListFloat attackPrefabSyncList;
    public SyncListFloat speedPrefabSyncList;
    public SyncListFloat mergePrefabSyncList;
    public SyncListFloat splitPrefabSyncList;

    public void Awake() {
        this.Initialize();
    }

    public void Initialize() {
        if (this.healthPrefabList == null) {
            this.healthPrefabList = new List<float>(10);
        }
        if (this.attackPrefabList == null) {
            this.attackPrefabList = new List<float>(10);
        }
        if (this.speedPrefabList == null) {
            this.speedPrefabList = new List<float>(10);
        }
        if (this.mergePrefabList == null) {
            this.mergePrefabList = new List<float>(10);
        }
        if (this.splitPrefabList == null) {
            this.splitPrefabList = new List<float>(10);
        }

        if (this.healthPrefabSyncList == null) {
            this.healthPrefabSyncList = new SyncListFloat();
            this.healthPrefabSyncList.Callback = this.OnSomethingChanged;
        }
        if (this.attackPrefabSyncList == null) {
            this.attackPrefabSyncList = new SyncListFloat();
            this.attackPrefabSyncList.Callback = this.OnSomethingChanged;
        }
        if (this.speedPrefabSyncList == null) {
            this.speedPrefabSyncList = new SyncListFloat();
            this.speedPrefabSyncList.Callback = this.OnSomethingChanged;
        }
        if (this.mergePrefabSyncList == null) {
            this.mergePrefabSyncList = new SyncListFloat();
            this.mergePrefabSyncList.Callback = this.OnSomethingChanged;
        }
        if (this.splitPrefabSyncList == null) {
            this.splitPrefabSyncList = new SyncListFloat();
            this.splitPrefabSyncList.Callback = this.OnSomethingChanged;
        }
    }

    public override void OnStartClient() {
        GameObject content = GameObject.FindGameObjectWithTag("Content");
        if (content != null) {
            Attributes attr = content.GetComponent<Attributes>();
            if (attr != null) {
                attr.unitAttributes = this;
                Debug.Log("Unit attributes set successfully.");

                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < Attributes.MAX_NUM_OF_LEVELS; j++) {
                        switch (i) {
                            case 0:
                                this.healthPrefabList.Add(1f);
                                break;
                            case 1:
                                this.attackPrefabList.Add(1f);
                                break;
                            case 2:
                                this.speedPrefabList.Add(1f);
                                break;
                            case 3:
                                this.mergePrefabList.Add(3f);
                                break;
                            case 4:
                                this.splitPrefabList.Add(3f);
                                break;
                        }
                    }
                }
            }
        }
    }

    public void UpdateValues() {
        if (!this.hasAuthority) {
            this.UpdateListValues();
            return;
        }
        CmdUpdateValues();
    }

    [Command]
    public void CmdUpdateValues() {
        RpcUpdateValues();
    }

    [ClientRpc]
    public void RpcUpdateValues() {
        Debug.Log("Writing values.");

        while (this.healthPrefabSyncList.Count < this.healthPrefabList.Count) {
            this.healthPrefabSyncList.Add(0f);
        }
        for (int i = 0; i < this.healthPrefabList.Count; i++) {
            this.healthPrefabSyncList[i] = this.healthPrefabList[i];
            Debug.Log("Value: " + this.healthPrefabList[i] + " Sync Value: " + this.healthPrefabSyncList[i]);
        }

        while (this.attackPrefabSyncList.Count < this.attackPrefabList.Count) {
            this.attackPrefabSyncList.Add(0f);
        }
        for (int i = 0; i < this.attackPrefabList.Count; i++) {
            this.attackPrefabSyncList[i] = this.attackPrefabList[i];
        }

        while (this.speedPrefabSyncList.Count < this.speedPrefabList.Count) {
            this.speedPrefabSyncList.Add(0f);
        }
        for (int i = 0; i < this.speedPrefabList.Count; i++) {
            this.speedPrefabSyncList[i] = this.speedPrefabList[i];
        }

        while (this.mergePrefabSyncList.Count < this.mergePrefabList.Count) {
            this.mergePrefabSyncList.Add(0f);
        }
        for (int i = 0; i < this.mergePrefabList.Count; i++) {
            this.mergePrefabSyncList[i] = this.mergePrefabList[i];
        }

        while (this.splitPrefabSyncList.Count < this.splitPrefabList.Count) {
            this.splitPrefabSyncList.Add(0f);
        }
        for (int i = 0; i < this.splitPrefabList.Count; i++) {
            this.splitPrefabSyncList[i] = this.splitPrefabList[i];
        }


        Debug.Log("Updating value.");

        for (int i = 0; i < this.healthPrefabList.Count; i++) {
            this.healthPrefabList[i] = this.healthPrefabSyncList[i];
        }

        for (int i = 0; i < this.attackPrefabList.Count; i++) {
            this.attackPrefabList[i] = this.attackPrefabSyncList[i];
        }

        for (int i = 0; i < this.speedPrefabList.Count; i++) {
            this.speedPrefabList[i] = this.speedPrefabSyncList[i];
        }

        for (int i = 0; i < this.mergePrefabList.Count; i++) {
            this.mergePrefabList[i] = this.mergePrefabSyncList[i];
        }

        for (int i = 0; i < this.splitPrefabList.Count; i++) {
            this.splitPrefabList[i] = this.splitPrefabSyncList[i];
        }
    }

    public void UpdateListValues() {
        for (int i = 0; i < this.healthPrefabList.Count; i++) {
            this.healthPrefabList[i] = this.healthPrefabSyncList[i];
        }

        for (int i = 0; i < this.attackPrefabList.Count; i++) {
            this.attackPrefabList[i] = this.attackPrefabSyncList[i];
        }

        for (int i = 0; i < this.speedPrefabList.Count; i++) {
            this.speedPrefabList[i] = this.speedPrefabSyncList[i];
        }

        for (int i = 0; i < this.mergePrefabList.Count; i++) {
            this.mergePrefabList[i] = this.mergePrefabSyncList[i];
        }

        for (int i = 0; i < this.splitPrefabList.Count; i++) {
            this.splitPrefabList[i] = this.splitPrefabSyncList[i];
        }
    }

    public void OnSomethingChanged(SyncListFloat.Operation op, int index) {
        Debug.Log("Testing this: " + index);
    }
}

From the code given above, I may need to put Initialize() in the OnStartClient() override function.

Nothing’s changed, however. But I think I’m slowly going in the right direction with the OnStartClient().

The SyncListFloat is still reported as “Not Initialized” even when it’s already initialized on both the server and the client. This happens when I’m either Clearing() the sync list, or I’m Adding() new float values to the sync list.

Still welcoming anything.

I am not familiar with SyncLists in general, but since the UNet source code is now available, i took a peek at SyncList.cs (https://bitbucket.org/Unity-Technologies/networking/src/c26a8146968cbd61c2017b3e49327f3d2c3996fa/Runtime/SyncList.cs?fileviewer=file-view-default)

You can see 2 things by looking at the source file:

  • The error is shown when the list doesn’t have a reference to m_Behaviour (line 237)
  • The only way to initialize m_Behaviour is by calling InitializeBehaviour (line 229)

Since there’s no apparent call to InitializeBehaviour, i figured this is done using the UNetWeaver (you know… that part where they automagically generate and insert code into your scripts).

…And indeed i was right: when you use SyncListFloat, it will inject code into your Awake() method and call InitializeBehaviour (passing “this” as the behaviour).

I have no idea what went wrong for you, but you can peek into the compiled assemblies to try and gather some more info… Let me know if that helped out in any way :slight_smile:

I had the same problem. Turns out you cannot use SyncList during Awake(). You can use SyncList during Start(), but not Awake().

See comment from Unity Dev here.

It’s very interesting that you are able to “peek” at the Unity codes. Never thought of this at all. From the looks of it, I can just call on InitializeBehaviour() on a SyncList in a OnStartClient() method?

And thanks for sharing the Unity dev’s comment.

No i don’t think you should be calling that yourself… That’s documented as “an internal method”. It is only used with code generation so i don’t think you can use that yourself.

Back to square one then.

Other developers seemed to have problems just to initialize SyncList. Because the problems are similar, yet the solutions vastly differs, I couldn’t tell which solution is the more consistent method of initializing SyncList.

I think you should just initialize it in any way that you’d like to. If that doesn’t work, you should consult the documentation, or the documentation should be modified to reflect the correct usage of this class.

Doc article has been updated after this thread has been created.