I made a script that duplicates an enemy that I have in my scene so that there are a lot of them instead of just one. However, when I destroy the original enemy, the duplication script no longer works because the gameobject is missing. How would I work around this?
here is the duplication script, by the way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : MonoBehaviour
{
public GameObject enemytospawn;
public Transform spawnplace;
IEnumerator task()
{
Instantiate(enemytospawn, spawnplace.position, spawnplace.rotation);
yield return new WaitForSeconds(5.0f);
StartCoroutine(task());
}
void Awake ()
{
StartCoroutine(task());
}
}
of course if you destroy the original enemy which is “enemytospawn”… it will become null therefore your instantiate script will not work… destroy the instances you make not the original…
Put the your enemy in project inside a folder as a prefab (it will become a blue box inside project)…
then drag drop it in the spawn script inside the scene (the spawn script should have its own gameobject)… so now your script only create clones from the prefab, and the prefab reside inside your project unharmed…