Question by
ronstanovsky05 · Apr 21, 2019 at 06:42 PM ·
destroy objectdestroy-clones
C# How to destroy a prefab clone ?
Greetings everybody, I did a spawner that spawns enemies. but the problem is it spawns the prefab enemy first and when im colliding with him no more enemies spawn, can you help me please? BTW its Unity2D
I got 2 codes: Spawner Code:
public GameObject enemy;
public float TimeBtwSpawn;
public float SpawnTime;
void Start() {
TimeBtwSpawn = SpawnTime;
}
void Update()
{
if (TimeBtwSpawn <= 0)
{
Instantiate(enemy, transform.position, Quaternion.identity);
TimeBtwSpawn = SpawnTime;
}
else {
TimeBtwSpawn -= Time.deltaTime;
}
}
Enemy Code:
public Rigidbody2D rb;
public float speed;
public GameObject effect;
public float EnemyHealth;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = Vector2.right * speed ;
}
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Player")) {
Destroy(gameObject);
}
}
Comment
Answer by xxmariofer · Apr 22, 2019 at 11:06 AM
thats probablyt because you are destroying the enemy object that is the objects being used for creating new instance, just drag and drop your enemy to your project folder, so you create a prefab, delete the enemy from the scene and use the prefab as enemy object