SyncListString behave strange

I have this code:

[Command]
    void Cmd_DeleteFromList (int _indx) {
        print ("1) syncLC: " + syncLC.cardSyncList.Count);

        if (!hasAuthority)
            return;

        print ("2) syncLC: " + syncLC.cardSyncList.Count + " - _indx: " + _indx);

        syncLC.cardSyncList.RemoveAt (_indx); <<< ERROR here

        print ("3) syncLC: " + syncLC.cardSyncList.Count);
    }

I get the following output from the Print’s:

1) syncLC: 52
2) syncLC: 52 - _indx: 51
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index

I have checked and the record at index 51, the last one as it starts at 0, is there prior to execute this code. It cast the error when i run the .RemoveAt code. After the out of range is cased it has removed the last record (_indx = 51) from the SyncListString but still cast the error. print #3 is never executed.

This happens both when i test only with one client and with multiple clients. It also happens when i use _indx = 10 (or any other number).

It never cast a SyncList .RemoveAt according to my callback: " SyncListString.Operation_op" as it does the add’s. …still the last record in the SyncList is removed. Hmmm. it feels the I do the RemoveAt locally but doesn’t execute in the SyncList framework?

I do not understand what i do wrong?

Problem Solved:
It was the callback that was the problem. When i executed the .RemoveAt the index at the callback did not exist any longer and casted the error. Because it was the callback, and it is a background job, the error was casted so it looked like it was my .RemoveAt code.

Maybe this is something for Unity to look at as it basically means that i have to either remove the call back of fiddle around if an if statement, in the callback, to avoid this to happen.

So i changed this:

private void SyncListCallBack (SyncListString.Operation _op, int _index) {

        if (_index > 0) {
            string _str = "Op=" + _op.ToString () + " Index=" + cardSyncList [_index] + " New Array Length=" + cardSyncList.Count;

            print (_str);
        }

    }

To this:

private void SyncListCallBack (SyncListString.Operation _op, int _index) {

        string _str = "";

        if (_index > 0 && _index > cardSyncList.Count) {
            _str = "Op=" + _op.ToString () + " Index=" + cardSyncList [_index] + " New Array Length=" + cardSyncList.Count;
        } else if (_index > 0) {
            _str = "Op=" + _op.ToString () + " New Array Length=" + cardSyncList.Count;
        }
        print (_str);
    }
1 Like