Removing and Re-adding an object

When I try this it removes the object but it doesn’t come back after 1 second?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flicker : MonoBehaviour {

    void Start()
    {
        StartCoroutine(Flash());
    }

    IEnumerator Flash()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f);
            Destroy(this.gameObject);
        }
    }
}

The object gets destroyed so it no longers exists in the scene. Once it is destroyed, the coroutine is no longer executed either. I’m guessing you want to show/hide the object. You don’t want to do this on the object you want to show/hide as it will stop calling the Update method or running a coroutine. You will likely be better off enabling/disabling the renderer on the object or running the method from another object.

2 Likes

Even if the coroutine didn’t stop… what about that code would “bring the object back”? You would need an Instantiate call somewhere.

1 Like