Generic arguments can only take actual types. However the generic version of AddComponent is just a wrapper around the actual AddComponent method. That’s the one you want to use. The generic method just does this:
public T AddComponent<T>()
{
return AddComponent(typeof(T)) as T;
}
The “actual” AddComponent method is not generic and takes a System.Type reference as an actual parameter. So you want to do
You essentially changed the question now and with the provided information we can not answer it. Are you sure that the type in question:
is actually derived from MonoBehaviour
is located in it’s own file and the file name matches the class name, or the type is located in a loaded assembly?
Do you actually enter your if statement? Keep in mind that Type.GetType will only find types from the executing assembly. If you use assembly definition files or if that class is allready located in a separate assembly, just using Type.GetType may not work. For that you need an assembly qualified type name that includes the assembly name or directly look for the Assembly and search for the type there.
May I ask why you actually want to use an unreliable string binding to add your component? Why don’t you use the actual type? Is there a reason to go through the slow and error prone reflection method? Since it seems you placed your class in a namespace, you would simply do
AddComponent<MyGame.SceneObjectBridge>();
// or
AddComponent(typeof(MyGame.SceneObjectBridge));
Note that from a technical point of view the namespace is a fix part of the class / type name. inside code we can use “using” statements at the top in order to shorten type names in code and the compiler will figure out the actual class you want to refer to. That’s why you get an error when you have two namespaces included that both contain the same class name. For example there is the UnityEngine.Random class and there’s the System.Random class. When you import both, the UntiyEngine and the System namespace in the same file and try to use the Random class, the compiler will throw an error as it can not determine which class you actually want to use.
Beacause I need create a lots of gameObject with different component in runtime.
Each type corresponds to a button in UI.
Use UI event sent type string to the reflection method.
I dont want write too much if/else and switch/case.
Ok, that’s perhaps a legit use. Make sure you do this only once however. You can cache the types like this so can save yourself from having to get results by reflection all the time.
// using System.Collections.Generic;
Dictionary<string, Type> _cache; // class member
Type getType(string typeName) {
_cache??= new Dictionary<string, Type>();
Type type;
if(!_cache.TryGetValue(typeName, out type)) {
type = _cache[typeName] = Type.GetType(typeName);
}
return type;
}
This doesn’t save you from typing errors, so make sure your strings are good.
Now you can
string componentName = "MyGame.SceneObjectBridge";
var componentType = getType(componentName);
Type.GetType will get called only the first time you use this, so we trade memory for performance. (Just to be in the clear: individual string length doesn’t affect the speed btw and it doesn’t slow down if you have many strings.)