My list isn't sorting by distance correctly...why?

using System.Linq;
using System.Collections.Generic;

public class listattempt : MonoBehaviour {
   
using System.Linq;
using System.Collections.Generic;

public class listattempt : MonoBehaviour
{
    public List<GameObject> m_targetList = new List<GameObject>();
    public GameObject[] houses;
    // Use this for initialization
    void Start()
    {
        houses = GameObject.FindGameObjectsWithTag("obstacle");
        m_targetList.AddRange(houses);

    }

    // Update is called once per frame
    void Update()
    {
        m_targetList.Sort(delegate (GameObject c1, GameObject c2) {
            return Vector3.Distance(this.transform.position, c1.transform.position).CompareTo
                        ((Vector3.Distance(this.transform.position, c2.transform.position)));
        });

    }
}

trying to sort the list of GameObjects by distance from an enemy. “houses” is an array of them. I don’t get any error, but the list (which is definitely updates based on my position) isn’t sorted correctly or even in any pattern I can notice. The end goal is to get the second-closest object.

I tried using an example from the docs but it’s returning the wrong house now.

using System.Linq;

public class anothertrydamnit : MonoBehaviour
{

    public GameObject house;


    GameObject GetNearestTarget()
    {
        //so lets say you want the closest target from a array (in this case all Gameobjects with Tag "enemy") and let's assume this script right now is on the player (or the object with which the distance has to be calculated)
        return GameObject.FindGameObjectsWithTag("obstacle").Aggregate((o1, o2) => Vector3.Distance(o1.transform.position, this.transform.position) > Vector3.Distance(o2.transform.position, this.transform.position) ? o2 : o1);
    }

    void Start ()
    {
        house = GetNearestTarget();

    }

}

No one’s replying… Is the code correct and the problem is outside the script?

ok so I tried a few things, and found what the problem is, but not sure how to fix it. I tested it out by switching the obstacles’ tag to something else and gave a cube that I duplicated the tag “obstacle.” and it works perfectly with the cube. It’s the object with the tag “obstacle” that is messing it up. changing the tag doesn’t help, but I wonder if a re-import would help.

Has this ever happened to someone before where the objects caused the program to glitch out? I even tried reinstalling and it still didn’t help.

yup it all works perfectly with the new gameobject environment. I didn’t try re-importing the old set of environment models, though I did try making a new instance of each, and it didn’t work. How do I mark a topic as Solved?

You should be able to edit the title.

This is the problem with using tags. You have probably forgotten to add the tag to one of your “houses”, or added it to one to many, or something like that.

Search your scene for all houses…

I think in the hierarchy window there’s a search text box, just type ‘obstacles’ in there… I’m not in front of my unity dev machine right now, but if it doesn’t do search by tag, you can write one (I’ve written a few search tools for my artist, but it was a while ago, I don’t remember if I had to write a ‘by tag’ one or not).

Anyways, find all the objects with the ‘obstacles’ tag, make sure you’ve set them up correctly.