Re: Unity tutorial on Lists, not all code paths return a value

I followed the unity tutorials on lists. Aside from some variable name differences, I have copied the code exactly into VS2015 and cannot seen to get it to work. The code is simple: check to see if there is a value present in the other list, if not set to “-1” and for all other circumstances set the value to match the second list. VS pops an error " ‘DefineDropList.CompareTo(DefineDropList)’ : not all code paths return a value. Any insight would be greatly appreciated.

public class DefineDropList : IComparable<DefineDropList>
{
    public string name;
    public int trophyenabled;

    public DefineDropList(string newName, int newTrophyEnabled)
    {
        name = newName;
        trophyenabled = newTrophyEnabled;
    }

    public int CompareTo(DefineDropList other)
    {
        if (other == null)
        {
            trophyenabled = -1;
            return trophyenabled;
        }
        trophyenabled = other.trophyenabled;
        return trophyenabled;
    }
}

Thats not how IComparable is meant to be used, its meant to help a classes know which object (this or the other) is greater. positive number mean the “other” is greater, while negatives means its lesser than this instance. zero means “this” and “other” are equal. anything can use the IComparable to make comparisons between two objects, but typically built-in Sorting methods use it the most

You shouldn’t have the CompareTo directly modifying any class variables since you don’t know exactly how a sort method will iterate through each instance of your DefineDropList (unless you REALLY know what your doing, even then there is usually better ways than IComparable). There are a myriad of different types of sorts out there and many will often access the same instance’s CompareTo multiple times. with code like that you could have some sorts get caught in an infinite loop since the CompareTo wouldn’t return consistent values between the two same instances.

try a different function (or remove the IComparable interface if you’re not sorting this class)

    public static int GetOtherTrophy(DefineDropList other)
    {
        if(other == null)
            return -1;
    
        return other.trophyEnabled;
    }

Edit: Forgot that I didn’t quite answer your question. The code you shown us compiles properly. your specific issue can stem from other things you haven’t shown us. like what namespaces you are using, if the filename matches the classname, and maybe even re-syncing the solution to your project.