- Home /
How to respawn an enemy prefab after its destroyed
Hi! I wrote an enemy destroyed code. And how can I change this so that it respawns the enemy after 3 seconds at the same starting location?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(gameObject);
}
}
}
Rather than destroying the GameObject, try disabling it and then setting the position back to the Spawn point as Destroying and Creating objects is more expensive.
As for the timer - just try searching for "unity timer". First 2 hits I got were: https://answers.unity.com/questions/351420/simple-timer-1.html https://answers.unity.com/questions/225213/c-countdown-timer.html Hope this helps
Answer by KittenSnipes · Dec 19, 2017 at 02:55 PM
Another way you can go about it is when spawning the gameObjects do:
// Put the object to copy here for reference
public GameObject enemy;
public Vector3 newPosition = new Vector3(0, 0, 0);
void SpawnEnemies() {
GameObject clone = Instantiate(enemy, enemy.transform.position, enemy.transform.rotation);
//If you want to do anything to the copied object then do something like
clone.transform.position = newPosition;
}
Your answer
Follow this Question
Related Questions
Respawn target after destroying returns error 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
destroty gui object 1 Answer
Destroy a Gameobject with a UI Button 3 Answers