Check if child exists and instantiate as child

I have a Gas Can prefab that consists of a parent and a single child. The parent is an empty game object. The child is the visible animated gas tank, with a collider, and the script that raises the players fuel and deletes its self upon colliding with the player. I want another script that instantiates a new child after say 30 seconds have passed, right where the old child was, if the old child has already been destroyed. If there’s a better way to do this, I’d appreciate it. It’s kind of a primary aspect of my game.

This question is difficult to answer accurately without know more about your situation. In particular:

  • Is the child you want to spawn after 30 seconds also the gas can?
  • You say “if the child has been destroyed” which implies that the 30 second timer is triggered by something other than the destruction of the gas tank.
  • You say right where the old child was. Is that the same local position or the same world position (only makes a difference if the empty parent object moves)?

Let me assume that some event happens to ‘destroy’ the gas can. Might be hitting it, might be going empty, might be some trigger. And that you want the gas can to reappear at the same local position after 30 seconds. If so a good approach would be to hide the gas can rather than destroy it. The outline of the logic for the script might look like this:

function Hide() {
    collider.enabled = false;
    renderer.enabled = false;
    Invoke("Show", 30.0);

function Show() {
    collider.enabled = true;
    renerer.enabled = true;

And the script would obviously go on the gas can. You would call Hide() when whatever event occurs that ‘destorys’ the gas can.