Generic method and factory pattern

Hi, I want to instantiate dynamicaly object with a generic factory pattern.
The object must be inherit Enemy class.
I cant pass GetType() in my generic method
Thanks

`
public class Factory : MonoBehaviour {

    public GameObject basic;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {

}

public void Build<T>() where T : Enemy, new()
{
    T t = new T();

    GameObject go = (GameObject)Instantiate(basic,Vector3.zero, Quaternion.identity);
    go.name = t.GetType().ToString();
    go.GetComponent<MeshRenderer>().material = Resources.Load(go.name) as Material;
    go.AddComponent<T>();

}

}

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Detector : MonoBehaviour {


    public List<Enemy> enemys = new List<Enemy>(); // content (Warrior, Paladin, Etc) 

    void OnTriggerEnter(Collider col)
    {
        if (col.tag == "Player")
        {
        
            Enemy e = enemys[Random.Range(0, 4)];
        
            GetComponent<Factory>().Build<e.GetType()>(); // Compiler doesnt want that
        

        }

   
    }
}
public class Mage : Enemy {

}
public class Warrior: Enemy {

}
public class Paladin: Enemy {

}

`

You can’t use generics like that. The passed type parameter must be a constant at compile time, so expressions aren’t allowed.

The easiest fix would be to not use generics at all and pass the type as a parameter instead

public void Build(System.Type type){
    if(!type.IsSubclassOf(typeof(Enemy))) return; //don't do anything if we didn't get an enemy-class
    GameObject go = (GameObject)Instantiate(basic,Vector3.zero, Quaternion.identity);
    go.name = type.ToString();
    go.GetComponent<MeshRenderer>().material = Resources.Load(go.name) as Material;
    go.AddComponent(type);
}

If you don’t want to, or unexpected complications arise in your design from sending types to the factory, you could probably use with an enum instead, since you have a limited number of classes and they are unlikely to change. Enums can be easily converted to strings and then to types( using System.Type.GetType(classname); )

An alternative would be to have a list of all accepted types in the factory. You could set it up in Awake, or use reflection to fill it.

Try using

go.AddComponent( go.name );

or more directly

go.AddComponent( t.GetType().ToString() );

The string must be the exact name of the component type you’d like to add.

edit: Ahh. I see. IMHO, a protected enum would probably be the simplest solution.

I found the solution with reflexion. Thx for responses @Jamora yes I think pass the parameters to System.Type is a good solution if we cant do with reflexion. and yes enum is pretty good way if you dont have lot of class.
Bye

 int result = Random.Range(0, enemys.Count);

            var t = typeof(Factory);
            var e = enemys[result];
            var m = t.GetMethod("Build");
            var build = m.MakeGenericMethod(e.GetType());
            var factory = GetComponent<Factory>();
            build.Invoke(factory,null);