How attach C# script by argument?

I am not native, so I might write strange English. sorry.

I want to attach the script chosen by argument to GameObject.

Example, when argument = “test1” , I want to attach “test1.cs”. when argument = “test2” , I want to attach “test2.cs”.

I tried this
public void ScriptAttacher(string StringArgument){
GameObject obj = GameObject.Find(“objectName”);
obj.AddComponent();
}

but it didn’t work.

How can I implement this function?

thank you.

AddComponent receiving a string parameter is obsolete.
Instead of string parameter, you should add the Type of the class you wish to add.
Type.GetType() might help you with your problem.

obj.AddComponent<Type.GetType(StringArgument)>();

I’ve never done this, but try :

// add `using System;` at the top of your file

public void ScriptAttacher(string stringArgument)
{
    try
    {
        GameObject obj = GameObject.Find("objectName");
        obj.AddComponent( Type.GetType(stringArgument) as Component );
     }
     catch (TypeLoadException e)
     {
        Debug.LogError("Unable to attach component " + stringArgument);
     }
}

@Dragate
obj.AddComponent<Type.GetType(StringArgument)>();
this, Unity says “Unexpected symbol ‘;’”
_

@Hellium
obj.AddComponent( Type.GetType(stringArgument) as Component );
this, Unity says “Cannot convert type ‘System.Type’ to ‘UnityEngine.Component’ via a built-in conversion”

I tried this
obj.AddComponent (Type.GetType (stringArgument));
this worked well, tnak you very much.

What should I do for your answers here?