Searching for object by attribute.

Let’s say that I have a game object called thing, with a script attached to it called Health, and it’s tag set to “Thing”.
Lets say the script contains 1 variable, and at it’s value changes from thing, to thing.

public float health;

Now let’s say that there are several of the same game object (Thing) present in the scene at one time, clones perhaps, and I wanted to find out which game object (Thing) had the most health from another script at any given time. This new script would hold something like this: (the area with the comment is what I cannot figure out).

GameObject thing;
	GameObject ThingWithMostHealth() 
	{
		GameObject[] theThing;
		theThing = GameObject.FindGameObjectsWithTag("Thing");
		foreach (GameObject aThing in theThing) 
		{
			//How do I find the thing with most health?
		}
		return thing;
	}

Thanks for any help!

You could do this:

Make sure to include using System.Linq.

Health[] healths = Object.FindObjectsOfType(typeof(Health)) as Health[];
Health highestHealth = healths.MaxBy( h => h.myHealth ); // I dont know what your health param is called

Alternatively using your method you would need to call GetComponent on each of your GameObjects to get the Health component. Like so:

GameObject thing;
GameObject ThingWithMostHealth() 
{
	GameObject[] theThing;
    Health highest = null;
	theThing = GameObject.FindGameObjectsWithTag("Thing");
	foreach (GameObject aThing in theThing) 
	{
		Health h = aThing.GetComponent<Health>();
        if( highest == null || h.value > highest.value )highest = h;
	}
	return thing;
}