# Respawning Prefabs

**URL:** https://discussions.unity.com/t/respawning-prefabs/796853
**Category:** Asset & Content Management
**Tags:** Prefabs
**Created:** [June 21, 2020, 2:16am UTC](https://discussions.unity.com/t/respawning-prefabs/796853 "2020-06-21T02:16:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DeLaBerni](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@DeLaBerni](https://discussions.unity.com/u/DeLaBerni)
#### Post date: [June 21, 2020, 2:16am UTC](https://discussions.unity.com/t/respawning-prefabs/796853/1 "2020-06-21T02:16:45Z")

</div>

Hello Guys im pretty new at Unity and working now on my first 3D RPG game! I have one problem and im pretty sure someone can help me here.

Im working on my gather profession where I can gather plants and respawn them after a delay … all works pretty good but I have one problem. How I can store the positions to respawn them ? I have it know when I gather something and I respawn it it works and spawn on the exact position. When I have 2x plants and I wait until one is respawned it works too. But if I gather one and then I gather again one I have the problem that only 1 “plant” is respawned because he resets the bool and the position … Maybe someone can help me … Here is my code …

```csharp
    [SerializeField] private GameObject plant;

    private Vector3 spawnPosition;

    private bool isDestroyed = false;
    private float respawnTimer = 0f;
    private float waitTime = 10f;

    private void Update()
    {
        if (isDestroyed)
        {
            respawnTimer += Time.deltaTime;

            if (respawnTimer > waitTime)
            {
                Respawn();
                respawnTimer = 0f;
            }
        }
    }

    public void Despawn(GameObject go)
    {
        spawnPosition = GetComponentInChildren<GatherLootTable>().MyStartPosition;
        isDestroyed = true;
        Destroy(go);
    }

    private void Respawn()
    {
        GameObject go = Instantiate(plant, spawnPosition, Quaternion.identity);
        go.transform.parent = GameObject.FindWithTag("Botaniker").transform;
        isDestroyed = false;
    }

```

---

<div class="post-metadata">

### Author: ![DeLaBerni](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@DeLaBerni](https://discussions.unity.com/u/DeLaBerni)
#### Post date: [June 21, 2020, 2:57am UTC](https://discussions.unity.com/t/respawning-prefabs/796853/2 "2020-06-21T02:57:10Z")

</div>

I changed now the code, the problem I have now is that my coroutine spawn endless my plants after the delay of 10 seconds … the bool is working correct what I did false ?

```csharp
public class Botaniker : MonoBehaviour
{
    [SerializeField] private GameObject plant;

    private Vector3 spawnPosition;

    private bool isDestroyed = false;
    private float waitTime = 10f;

    private void Update()
    {
        if (isDestroyed)
        {
            StartCoroutine(Respawn());
        }
    }

    public void Despawn(GameObject go, Vector3 respawn)
    {
        spawnPosition = respawn;
        isDestroyed = true;
        Destroy(go);
    }

    private IEnumerator Respawn()
    {
        yield return new WaitForSeconds(waitTime);
        GameObject go = Instantiate(plant, spawnPosition, Quaternion.identity);
        go.transform.parent = GameObject.FindWithTag("Botaniker").transform;
        isDestroyed = false;
    }
}

```

---

<div class="post-metadata">

### Author: ![dahiyabunty1](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dahiyabunty1](https://discussions.unity.com/u/dahiyabunty1)
#### Post date: [June 21, 2020, 3:11am UTC](https://discussions.unity.com/t/respawning-prefabs/796853/3 "2020-06-21T03:11:57Z")

</div>

StartCoroutine(Respawn());  
isDestroyed = true;
