C# string as type for AddComponent

Hello,

I am making a custom Editor Window where I can create quests.
The tool generates a .cs file using streamwriter.
But when I want to assign the script in editor to an object (questGiver) it can’t use the string as a type.

using UnityEditor;
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;

[ExecuteInEditMode] 
public class Quest : EditorWindow 
{
	public static int levelRequirement = 0;
	public static string questName ="Enter Questname Here";
	public static int questExp = 0;
	public static string questGiver = "Quest Giver";
	public static GameObject selectedObject;
	public float secs = 10f;
	public float startVal = 0f;
	public float progress = 0f;
	public class curQuest : MonoBehaviour{};

	//QuestStates:
	//	-1	Player is too low level for the quest so the questgiver denies the request
	//	-2	Player activates the quest
	// 	-3	Player has not yet finishes the quest
	//	-4 	Player has failed the quest but he can start over
	//	-5 	Player finishes the quest

	public static string questState_1_String = "Can not begin Quest now";
	public static string questState_2_String = "Here is your Quest";
	public static string questState_3_String = "You're not finished yet";
	public static string questState_4_String = "Please try again";
	public static string questState_5_String = "Get your loot";

	[MenuItem ("Window/Quests")]
	static void Init () {
		// Get existing open window or if none, make a new one:
		Quest window = (Quest)EditorWindow.GetWindow (typeof (Quest));
		window.Show();
	}

	void OnGUI(){
		questName = EditorGUILayout.TextField ("Questname", questName);
		questGiver = Selection.activeTransform ? Selection.activeTransform.name : "Select Quest Giver";
		EditorGUILayout.LabelField("Quest Giver: ", questGiver);
		levelRequirement = EditorGUILayout.IntSlider ("Minimum Level", levelRequirement, 0, 100);
		questState_1_String = EditorGUILayout.TextField ("State 1 Text", questState_1_String);
		questState_2_String = EditorGUILayout.TextField ("State 2 Text", questState_2_String);
		questState_3_String = EditorGUILayout.TextField ("State 3 Text", questState_3_String);
		questState_4_String = EditorGUILayout.TextField ("State 4 Text", questState_4_String);
		questState_5_String = EditorGUILayout.TextField ("State 5 Text", questState_5_String);
		questExp = EditorGUILayout.IntSlider ("Experience Points", questExp, 0, 1000);

		if (GUILayout.Button ("Add Quest")) {
			Create ();

		}

		EditorGUILayout.LabelField ("Compiling:", EditorApplication.isCompiling ? "YES" : "NO");

		if (!EditorApplication.isCompiling) {
			if (GUILayout.Button ("Add Quest to Questgiver")) {
				AddScript ();
			}
		}
	}

	static void Create()
	{
		string copyPath = "Assets/Quests/" + questName + ".cs";
		if (File.Exists (copyPath) == false) { // do not overwrite
			using (StreamWriter outfile = 
				       new StreamWriter (copyPath)) {
				outfile.WriteLine ("using UnityEngine;");
				outfile.WriteLine ("using System.Collections;");
				outfile.WriteLine ("");
				outfile.WriteLine ("public class " + questName + " : MonoBehaviour {");
				outfile.WriteLine (" ");
				outfile.WriteLine ("public int levelRequirement = " + levelRequirement + ";");
				outfile.WriteLine (" ");
				outfile.WriteLine (" // Use this for initialization");
				outfile.WriteLine (" void Start () {");
				outfile.WriteLine (" ");
				outfile.WriteLine (" }");
				outfile.WriteLine (" ");         
				outfile.WriteLine (" ");
				outfile.WriteLine (" // Update is called once per frame");
				outfile.WriteLine (" void Update () {");
				outfile.WriteLine (" ");
				outfile.WriteLine (" }");
				outfile.WriteLine ("}");
			}//File written
		}
		AssetDatabase.Refresh ();
	}
		void AddScript(){
		GameObject selectedObject = Selection.activeTransform.gameObject;
		selectedObject.AddComponent<questName>();
	}

	void OnInspectorUpdate(){
		Repaint ();
	}
}

The error is here in:

 void AddScript(){
         GameObject selectedObject = Selection.activeTransform.gameObject;
         selectedObject.AddComponent<questName>();
     }

This is my compiler error:

Assets/Editor/Quest.cs(97,45): error CS0118: `Quest.questName' is a `field' but a `type' was expected

It can’t use questName as a Type for AddComponent.
How should I add the script to the GameObject?

Greetings,

GerardM

reflection is your friend
take note that reflection is not stable with webgl and if my memory is correct, IOS builds have limitation on it to

here is how to get a type from a string

If I undestand what you intend to do, you could use the string overload of AddComponent I guess…

Like so:

selectedObject.AddComponent(questName);

Internally this most likely uses reflection, but why reinvent the wheel? :stuck_out_tongue: