Yeah, it’s me again, this time with a slightly more advanced question. I have an enemy class from which a bunch of enemy types come from. See code:
public class Enemy<T> where T : Enemy {
public GameObject gameObject;
public T ScriptComponent;
public Enemy(string name) {
gameObject = new GameObject (name);
ScriptComponent = gameObject.AddComponent<T> ();
}
}
public class Jackass : Enemy {
public override void Initialize(float healthpoints_, int healthNotches_, float speed_, float acceleration_, Quaternion rotation_, Vector3 position_){
healthPoints = healthpoints_;
healthNotches = healthNotches_;
speed = speed_;
transform.rotation = rotation_;
transform.position = position_;
}
protected override void MovementPattern () {
}
}
public class PistolJackass : Enemy {
public override void Initialize(float healthpoints_, int healthNotches_, float speed_, float acceleration_, Quaternion rotation_, Vector3 position_){
healthPoints = healthpoints_;
healthNotches = healthNotches_;
speed = speed_;
transform.rotation = rotation_;
transform.position = position_;
}
protected override void MovementPattern () {
}
}
It continues on like this for 13 different enemies so far.
I also have my spawner script, which uses reflection to get a list of all the enemies that can be spawned and puts them nicely in a popup box.
public class EnemyFactory : MonoBehaviour {
[HideInInspector]
public int selectedEnemy;
public float healthPoints;
public int healthNotches;
public float speed;
public float acceleration;
void Awake () {
var type = typeof(Enemy);
var listOfDerivedClasses = Assembly.GetExecutingAssembly ().GetTypes ().Where (x => x.IsSubclassOf (typeof(Enemy))).ToList ();
for (int i = 0; i < listOfDerivedClasses.Count; i++) {
// Debug.Log (listOfDerivedClasses [i].Name.ToString ());
}
Debug.Log(selectedEnemy);
Enemy<Jackass> placedEnemy = new Enemy<Jackass> (listOfDerivedClasses[selectedEnemy].Name.ToString());
placedEnemy.ScriptComponent.Initialize(
healthNotches_: healthNotches,
healthpoints_: healthPoints,
speed_: speed,
acceleration_: acceleration,
rotation_: Quaternion.identity,
position_: transform.position);
}
}
This works so long as I only want to spawn the Jackass enemy variant, but I have to hard code everything into enums if I want to have a dropdown menu that encompasses all enemy types. This is less than ideal because I’d like to have the ability to easily remove/add new enemies as time goes on.
I’ve looked into reflection for this, as it seems to be the way to go. I don’t have to worry too much about speed since this is just to spawn in enemies as the level launches. I won’t be calling this every frame or anything ridiculous like that.
So far I’ve come across loads of variants of the following code:
MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);
But for the life of me I can’t get it to work without getting some sort of null reference exception. Does anyone here know what I should be doing?