How to make 3d object reappear after disappearing ?

Hi everyone,

I’m pretty new to both game developing and C# programming although i have some basic knowledge of C and C++. Nevertheless, can someone help me out ? I got stuck today and i don’t know how to continue from this point. Basically I’m creating an environment full of bomb-like objects that spawn at random locations and then ‘explode’, but now i want to make them reappear again and repeat the cycle until its game over. If it can help you i can share my spawn and disappear scripts to see whats going on but you’ll probably get it. So can anyone show me how to make the cycle repeat until its game over every few seconds after disappearing.

Thanks in advance and peace !

Depends on how you made it disappear.

If you just set renderer.enabled = false, then it’s as simple as setting renderer.enabled to true again. However, your invisible object would still be interacting with the world, so this is not ideal.

If you destroyed it, you will have to spawn a new copy when it’s time to reappear. If you set gameObject.active = false, then it cannot run any scripts while it’s inactive, so another object will have to keep time. In either of these cases, you need a manager class - the object itself can’t keep time when it’s inactive or destroyed.

I’d recommend a singleton for this - a class that has a static reference in a non-static instance of itself in the scene. This works kinda like Camera.main, where you can always find that one thing no matter where in the code you are. That singleton can run coroutines, waiting for the right time. (The code below assumes you use the active=false method above)

public class ManagerClass : MonoBehaviour {
//singleton code
public static ManagerClass main;
void Awake() {
main = this;
}

//the function
public void RespawnAfterDelay(GameObject thing, float delay) {
StartCoroutine(RespawnHelper(thing, delay));
}
//the coroutine
private IEnumerator RespawnHelper(GameObject thing, float delay) {
yield return new WaitForSeconds(delay);
thing.SetActive(true);
}

//elsewhere
gameObject.SetActive(false);
ManagerClass.main.RespawnAfterDelay(gameObject, 2f);

Sorry for replying this late, was busy, I finally had some time to get back to my game and i tried your script but it didn’t work. I think i cant work due to my Destroy script which is here :

public class Detonate : MonoBehaviour {

    var destroyTime = 3;

    function Update () {
        Destroy(gameObject, destroyTime);
    }
}

If you can help somehow or at least give me a tip on making this work I’d be thankful.

You seem to be mixing c# and unityscript syntax. Also I dont see why using StarManta’s code doesnt work, but if you want something to reappear destroying it isnt the best option.