Respawn After Destroy game object unity
You’re running into a classic Unity pattern: you destroy a GameObject, and then you want it to respawn repeatedly. Unity won’t automatically recreate destroyed objects—you have to explicitly spawn them again.
There are two clean ways to handle this depending on how often you need to respawn.
🔁 Simple Respawn After Destroy
If you only need to respawn occasionally, a coroutine works beautifully.
Spawner.cs
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject prefab;
public float respawnDelay = 2f;
private GameObject currentObject;
void Start()
{
Spawn();
}
void Spawn()
{
currentObject = Instantiate(prefab, transform.position, Quaternion.identity);
}
public void ObjectDestroyed()
{
StartCoroutine(Respawn());
}
System.Collections.IEnumerator Respawn()
{
yield return new WaitForSeconds(respawnDelay);
Spawn();
}
}
On the object that gets destroyed
public class DestroyNotifier : MonoBehaviour
{
public Spawner spawner;
void OnDestroy()
{
if (spawner != null)
spawner.ObjectDestroyed();
}
}
This setup respawns the object every time it’s destroyed.
🚀 Better for Many Repeated Spawns: Object Pooling
If you’re spawning/destroying constantly (bullets, enemies, obstacles), destroying objects repeatedly causes garbage collection spikes. Instead, disable and reuse them.
ObjectPool.cs
using UnityEngine;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour
{
public GameObject prefab;
public int poolSize = 10;
private List<GameObject> pool = new List<GameObject>();
void Start()
{
for (int i = 0; i < poolSize; i++)
{
GameObject obj = Instantiate(prefab);
obj.SetActive(false);
pool.Add(obj);
}
}
public GameObject Get()
{
foreach (var obj in pool)
{
if (!obj.activeInHierarchy)
{
obj.SetActive(true);
return obj;
}
}
// Optional: expand pool
GameObject newObj = Instantiate(prefab);
pool.Add(newObj);
return newObj;
}
}
Spawner using the pool
public class Spawner : MonoBehaviour
{
public ObjectPool pool;
public float spawnDelay = 2f;
void Start()
{
StartCoroutine(SpawnLoop());
}
IEnumerator SpawnLoop()
{
while (true)
{
GameObject obj = pool.Get();
obj.transform.position = transform.position;
yield return new WaitForSeconds(spawnDelay);
}
}
}
This gives you infinite respawning without destroying anything.
🎮 Want the simplest possible version?
If all you want is:
“When this object is destroyed, spawn another one.”
Use this tiny script:
public class RespawnOnDestroy : MonoBehaviour
{
public GameObject prefab;
void OnDestroy()
{
Instantiate(prefab, transform.position, Quaternion.identity);
}
}
Attach it to the object you’re destroying.
Comments