list.indexof returns -1

I am trying to to return the index of item in list but its returning -1(Line # 16 collector script). Any help will be appreciated. Collector script is attached to collector game object.

public class Collector : MonoBehaviour
{
    #region Fields

    // targeting support
    SortedList<Target> targets = new SortedList<Target>();
    Target target= null;
void Start()
    {        EventManager.AddListener(listener);
        }
public void listener(GameObject pickup)
    {
        target = new Target(pickup, gameObject.transform.position);
       
        targets.Add(target);
       [B] [/B]Debug.Log("index of target " + targets.IndexOf(target));}

SortedList script is here:

public class SortedList<T> where T:IComparable
{
    List<T> items = new List<T>();

    List<T> tempList = new List<T>();
    public SortedList()
    {
       
    }
    public int Count
    {
        get { return items.Count; }
    }
    public T this[int index]
    {
        get { return items[index]; }
    }
    public void Add(T item)
    {
        int addLocation = 0;
        while((addLocation<Count)&&(items[addLocation].CompareTo(item)>0))
        {
            addLocation++;
        }
       
        if(addLocation == 0)
        {
           
            tempList.AddRange(items);
            items.Clear();
            items.Add(item);
            items.AddRange(tempList);
            tempList.Clear();

        }
        else
        {
            for(int i = 0; i < addLocation; i++)
            {
                tempList.Add(items[i]);
            }
            tempList.Add(item);
            for (int i = addLocation; i < items.Count; i++)
            {
                tempList.Add(items[i]);
            }
            items.Clear();
            items.AddRange(tempList);

}
        tempList.Clear();
       

    }
    public int IndexOf(T item)
    {
        int lowerBound = 0;
        int upperBound = items.Count - 1;
        int location = -1;

        // loop until found value or exhausted array
        while ((location == -1) &&
            (lowerBound <= upperBound))
        {
            // find the middle
            int middleLocation = lowerBound + (upperBound - lowerBound) / 2;
            T middleValue = items[middleLocation];

            // check for match
            if (middleValue.CompareTo(item) == 0)
            {
                location = middleLocation;
            }
            else
            {
                // split data set to search appropriate side
                if (middleValue.CompareTo(item) > 0)
                {
                    upperBound = middleLocation - 1;
                }
                else
                {
                    lowerBound = middleLocation + 1;
                }
            }
        }
        return location;
    }
}

Here is the Target Script:

public class Target : IComparable
{
    GameObject gameObject;
    float distance;

    public Target(GameObject gameObject, Vector3 position)
    {
        this.gameObject = gameObject;
        UpdateDistance(position);
        Debug.Log("targetConstructed");
    }
    public int CompareTo(object obj)
    {
        if (obj == null)
        {
            return 1;
        }

            // check object type
            Target otherTarget = obj as Target;
            if (otherTarget != null)
            {
            if (distance < otherTarget.distance)
            {
                    return -1;
                }
                else if (distance == otherTarget.Distance )
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
           
            }
       
            else
            {
                throw new ArgumentException("Object is not a target");
            }
        }

A few thoughts:

  • SortedList already exists in .net, so I’d recommend a different name if you are going to implement it yourself.

  • Don’t implement it yourself, since it’s already there in .net, a few options:

  • Use List and make it sorted by calling Sort. And then just use IndexOf of List.

  • Use the existing SortedList and make a wrapper that ignores the Value part. (SortedList is made for Key/Value pairs.)

2 Likes