Finding a Script That's attached to another GameObject in Runtime without knowing the ScriptName.

Hi,

So I made a Random Encounter Generator that Rolls dice to determine how many and what types of enemies your character will be fighting. Each different type of enemy has a specific script. It creates a GameObject and attaches the specific enemyType script to it and your player GameObjects starts fighting the enemy GameObjects, yada yada.

All the stat variables are stored in their specific scripts. So I need to figure out how to get those variables in runtime without knowing a specific script name because it could be any enemyScript.

The PlayerScript Targets an enemy GameObject like this:

public void GetClosestTarget(){
		GameObject[] objectsWithTag;
		objectsWithTag = GameObject.FindGameObjectsWithTag ("Enemy");

		foreach (GameObject go in objectsWithTag) {

			float distance = Vector2.Distance(go.transform.position, transform.position);

			if(distance < targetDistance){
				currentTarget = go;
			}
		}
	}

So my Players current Target is stored as a Gameobject in the currentTarget variable. I need to figure out what script is attached to that currentTarget GameObject. So I can access that enemies stats.

currentTarget.GetComponent<???>();

Basically I need to find out how to make a function that figures out what ??? is.

Okay sweet, I made a BaseCharacter class with base stats, and made a BaseGoblin class with a different method for each different goblin, declaring the base stats in each.

I attach the script to the new GameObject like so:

goblinPeon.AddComponent<Goblin>().GoblinPeon();

then if I want to change the values of my goblin peon I just call the baseCharacter class instead of the Goblin Class like so.

public void AttackTarget(){
		currentTarget.GetComponent<BaseEnemy>().EnemyHealth--;
}

So now, I can create any enemy type I want and to change the values I just change their BaseCharacter Class.

If there are no problems with this I think my problem is solved, thank you!