Duplicating a prefab

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 Spawners.

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.


Ok, now that we are clear about what you want to achieve I can give you a proper answer:

//This is what I would do

//You have your class everything derives from
public abstract class Spawnable : MonoBehaviour {
    //This will generate a set of data contained in one object
    public abstract Stats GetData();
    //This will apply a set of data to the current object
    public abstract void SetData(Stats stats);

//Then you have your spawner class
public class Spawner : MonoBehaviour {
    public Spawnable prefab;
    private Stats stats;
    //At the start we want to get stats which all our prefabs will use
    public void Start() {
        stats = prefab.GetData();
    public void Spawn() {
        Spawnable obj = (Spawnable)Instantiate(prefab, transform.position, transform.rotation);
        //when we spawn a prefab, we want to apply the spawners stats to it

Stats can really be anything. A string, a number etc. That is up to you how you plan on formatting that :wink:
This method will give you the functionality you need, along with the genericness you strive for.

Hope this helps,