access to a class from a string variable

Hey Guys

Im trying to set up a Modular Game. At the Moment i got 3 Scripts.

The Gamehandler should handle the game.
The Player1 is like a savefile. All the Equipment of the Player is saved there.
The Raptor file is the file of e Jet. All Variable like Health, Shield, Skin etc. is save there.

The Gamehandler should take the Players ship from the Player Skript. that works.
After that the Gamehandler should acces the right Ship (here it is the Raptor file) and take Health etc.
the problem is: The Ship in Player1 Skript is stored as a string. For GetComponent<>() it asks for a class.

Errormesage: Assets/Scripts/Gamehandler.cs(78,43): error CS0118: Gamehandler.Ship' is a field’ but a `type’ was expected

How can i acces these Variables?
Tanks for Help!

Here are the Scripts:


using UnityEngine;
using System.Collections;

public class Gamehandler : MonoBehaviour {
	public string Ship = null;	
	public int BaseHealth = 0;
	// Use this for initialization
	void Start () {
		LoadUserFile ();
	void LoadUserFile(){
		Ship = GetComponent<Player1> ().Ship;
		BaseHealth = GetComponent<Ship> ().Health;    


using UnityEngine;
using System.Collections;

public class Player1 : MonoBehaviour {

	public string Ship = "Fighter_Raptor";


using UnityEngine;
using System.Collections;

public class Fighter_Raptor : MonoBehaviour {

	public int Health = 3000; 	

Are you simply looking for the string version of GetComponent? The following two lines of code are equivalent. The second version is slower.

Ship ship = GetComponent<Ship>();

Ship ship =(Ship)GetComponent("Ship");

The compiler does not know what type GetComponent(Ship) returns other than component.

The closest I could get to solving your problem:

//Did some simplification when testing
String Ship = "Fighter_Raptor";
   Component shipComponent =  GetComponent(Ship);

   if (shipComponent.GetType() == typeof(Fighter_Raptor))
       Fighter_Raptor actualComponent = shipComponent as Fighter_Raptor;

   if (shipComponent.GetType() == typeof(SomeotherShipComponent))
       SomeotherShipComponent actualComponent = shipCopmonent as SomeotherShipComponent;