GetComponent(string) with unknown ScriptName


I got a little Problem with my GetComponent in my delegate…
I got an Array full with GameObjects, every GameObject has a Script with the Name: + “Points”
Now i made a Method that Sorts my List by checking the value of AtkSpeed inside the Scripts:

public void SortTargetsByAtks(List<GameObject> unitList){
		unitList.Sort(delegate(GameObject t1, GameObject t2){ 
			return((t1.GetComponent( +"Points").stats.AtkSpeed.CompareTo(t2.GetComponent( + "Points").stats.AtkSpeed)));			

This is the Error: error CS1061: Type UnityEngine.Component' does not contain a definition for stats’ and no extension method stats' of type UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)

I know i can use GetComponent using


, but the Problem is i don’t know the ScriptName…
I hope someone can help me, but i Thank you already for just reading my Problem =D

Try using an abstract class to accomplish this.

Step 1: Create a new interface:

  public abstract class HasStats: MonoBehaviour {
      Stat getStats();

Step 2: and then go through all your actual scripts (value of scriptname) and make them look like this:

  public class scriptname : HasStats

Step 3:


and that will do it.

if you want to make this more maintainable in the future you can take further advantage of Abstract classes. I can elaborate if people are interested. Anyways, the above should work for you, good luck!

Try casting it?