GetComponent when I don't know the name

In my code, I am instantiating a clone, then adding a script to it. The script I add is a random behavior script, but each behavior script will have certain variables I then want to change from the script that created the clone.

Right now I have it set like this:

clone = Instantiate (hazard, spawnPosition, spawnRotation) as GameObject;

//scriptAdder is a string with a random script name	

Now I want something like:

addedScript = clone.GetComponent<scriptAdder> ();

But obviously this doesn’t work as I can’t send in a string like this, as far as I know?

Anyone have an idea of how to accomplish this as I have it? I’m open to needing to reformat the way things are called if there’s no way to do what I’m looking to do, but any guidance would be much appreciated.


To add component at runtime use this :

	void Start () {
		this.gameObject.AddComponent ("YourScriptName");

Now to get this component Use:

YourScriptName script;

void Start(){
          script=this.GetComponent<YourScriptName>();  //use "this" if the script is on current gameobject else use the gameObjectName


You can use this trick to know which script you added:

YOURSCRIPTCLASS1 temp1 = this.GetComponent("YourScriptName1");
if (temp == null) {
  YOURSCRIPTCLASS2 temp1 = this.GetComponent("YourScriptName2");
} else {
   //Do what you want.