GetComponent(string) with unknown ScriptName

Hi,

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: GameObject.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(t1.name +"Points").stats.AtkSpeed.CompareTo(t2.GetComponent(t2.name + "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

GetComponent<ScriptName>().stats.AtkSpeed

, 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:

GetComponent<HasStats>().getStats().AtkSpeed

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?

((ScriptName)GetComponent(“BlahblashBlash”)).stats.AtkSpeed