Sharkstroke
Sharkstroke

Reputation: 82

Error: "Operation is not valid due to the current state of the object"

I have a huge problem at the moment in my Unity project. I'm trying to add a certain number of elements to a SortedList, but when I start the game I get this error:

error: "InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.SortedList2[TrisCoordinates,System.Boolean].PutImpl (.TrisCoordinates key, Boolean value, Boolean overwrite) System.Collections.Generic.SortedList2[TrisCoordinates,System.Boolean].Add (.TrisCoordinates key, Boolean value)"

Here is the code:

void AddCoords() {
    coords.Add (new TrisCoordinates (280.62f, 2.217f, 144.84f), false);
    coords.Add (new TrisCoordinates (280.65f, 0.9891f, 126.84f), false);
    coords.Add (new TrisCoordinates (285.42f, 0.9891f, 129.49f), false);
    coords.Add (new TrisCoordinates (281.64f, 2.9645f, 125.05f), false);
    coords.Add (new TrisCoordinates (263.00f, 2.2f, 103.27f), false);
    coords.Add (new TrisCoordinates (243.29f, 0.9891f, 101.96f), false); 
    coords.Add (new TrisCoordinates (243.10f, 0.9891f, 99.587f), false);
    coords.Add (new TrisCoordinates (233.20f, 1.018f, 93.414f), false);
    coords.Add (new TrisCoordinates (249.77f, 0.9891f, 112.95f), false); 
    coords.Add (new TrisCoordinates (247.87f, 0.9891f, 113.00f), false);
    coords.Add (new TrisCoordinates (242.59f, 0.9891f, 112.92f), false);
    coords.Add (new TrisCoordinates (240.59f, 0.9891f, 112.99f), false);
    coords.Add (new TrisCoordinates (236.85f, 0.9891f, 112.99f), false);
    coords.Add (new TrisCoordinates (234.79f, 0.9891f, 112.98f), false);
    coords.Add (new TrisCoordinates (248.88f, 0.9891f, 126.71f), false); 
    coords.Add (new TrisCoordinates (231.15f, 1.0182f, 148.53f), false);
    coords.Add (new TrisCoordinates (270.95f, 7.95f, 137.46f), false);
    coords.Add (new TrisCoordinates (278.60f, 7.95f, 137.60f), false);
    coords.Add (new TrisCoordinates (281.85f, 7.9292f, 137.60f), false);
    coords.Add (new TrisCoordinates (270.58f, 7.95f, 135.05f), false);
    coords.Add (new TrisCoordinates (264.75f, 9.8372f, 118.75f), false);
    coords.Add (new TrisCoordinates (256.27f, 7.93f, 103.37f), false); 
    coords.Add (new TrisCoordinates (205.63f, 1.037f, 407.70f), false);
    coords.Add (new TrisCoordinates (209.02f, 1.037f, 407.70f), false);
    coords.Add (new TrisCoordinates (216.61f, 1.037f, 407.70f), false);
    coords.Add (new TrisCoordinates (219.56f, 1.037f, 407.70f), false);
    coords.Add (new TrisCoordinates (226.32f, 1.037f, 403.22f), false);
    coords.Add (new TrisCoordinates (226.03f, 1.037f, 401.04f), false);
    coords.Add (new TrisCoordinates (225.98f, 1.037f, 394.01f), false);
    coords.Add (new TrisCoordinates (225.97f, 1.037f, 392.41f), false);
    coords.Add (new TrisCoordinates (202.00f, 1.037f, 392.56f), false);
    coords.Add (new TrisCoordinates (201.03f, 1.037f, 394.67f), false);
}

TrisCoordinates is a class so made:

public class TrisCoordinates {

    private float x;
    private float y;
    private float z;

    public TrisCoordinates(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public float GetX() {
        return x;
    }

    public float GetY() {
        return y;
    }

    public float GetZ() {
        return z;
    }

}

Any ideas about what causes it?

Upvotes: 3

Views: 3134

Answers (2)

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You use a sortedlist the program should be able to sort your list, which, as your class doesn't implement the IComparable, is impossible. You just need to inherits from it: public class TrisCoordinates : IComparable<TrisCoordinates> and implement the public int CompareTo(TrisCoordinates otherCoordinates) to make it works:

public class TrisCoordinates : IComparable<TrisCoordinates>
{
    public float X { get; private set; }
    public float Y { get; private set; }
    public float Z { get; private set; }

    public TrisCoordinates(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public int CompareTo(TrisCoordinates otherCoordinates)
    {
        if (X > otherCoordinates.X)
            return 1;
        if (X < otherCoordinates.X)
            return -1;
        if (Y > otherCoordinates.Y)
            return 1;
        if (Y < otherCoordinates.Y)
            return -1;
        if (Z > otherCoordinates.Z)
            return 1;
        if (Z < otherCoordinates.Z)
            return -1;
        return 0;
    }
}

Upvotes: 0

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

You should implement IComparable if you want to use the class as key for a SortedList, because to be able to sort, the sorted list should be able to compare two TrisCoordinates instances.

The below is a dummy comparison, but it should at least cause no exceptions in your code.

public class TrisCoordinates : IComparable<TrisCoordinates>
{

    private float x;
    private float y;
    private float z;

    public TrisCoordinates(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public float GetX()
    {
        return x;
    }

    public float GetY()
    {
        return y;
    }

    public float GetZ()
    {
        return z;
    }

    public int CompareTo(TrisCoordinates other)
    {
        return (x + y + z).CompareTo(other.x + other.y + other.z);
    }
}

Upvotes: 6

Related Questions