object respawn when player respawn

I use this .js to destroy objects after 5 seconds if the player pass near them

function OnControllerColliderHit(hit : ControllerColliderHit)
    if (hit.gameObject.tag == "Cube")  
            Destroy(hit.gameObject, 5);


What I want is to respawn this objects in the same position when Player respawn in the beginning of the scene

You destroy. You create.

  • Make a GameObject and name it like ObjectsCreator or something like this.
  • Create a script and attach in this object.
  • In this script, create Arrays that will save the position (and/or rotation) and informations about the destroyed object.
  • Create a function like AddDestroyedObject(informations) that will add the informations in the Arrays you previous created.
  • Create another function RespawnObjects() that will instantiate those objects based in the informations saved in the Arrays;
  • Clear the Arrays;
  • Call the function AddDestroyedObject() when destroy a object;
  • Call the function RespawnObjects() when your player respawn;

If you have more question about each step, make a new answer and make sure to be specific.

Another idea is:

  • Move the object 100000 units up (to hide then, instead destroy) and when respawn, all objects more than 10000 in y position get down…

Good Luck :)