I’m trying to create a Spawner
, that will spawn enemies around it’s location. Now, I’m trying to make this a generic Spawner
, so it receives a Spawnable
which is an abstract subclass of MonoBehaviour
, as parameter in it’s constructor, and is supposed to create objects from that Spawnable
template.
Now, each Spawnable
has a method that Instantiates a copy of self. This copy is not simple instantiation, becuase depending on the specific implementation of Spawnable
, it copies some of its parameters to the newly instantiated object.
public abstract class Spawnable : MonoBehaviour {
public abstract void Spawn(Vector3 position);
}
public class Enemy : Spawnable {
public float health;
public override void Spawn(Vector3 position) {
GameObject spawnedObject = (GameObject)Instantiate(this.gameObject, position, Quaternion.identity);
spawnedObject.GetComponent<Enemy>().health = this.health;
}
}
Now, say I have one prefab for Enemy
, but then I want to create different Spawner objects, each spawning enemies with different health. So I need several Enemy
prefabs, each one with different health, and pass them on as parameters to the Spawner
s.
Which brings us to my question: How do I duplicate the Enemy
prefab so I can have several instances with different health values? Or maybe any other ideas? It’s important to me to keep these generic and not limited to specific NPC implementations.