- Home /
After destroying an object, I need it to respawn in a random area a couple seconds after the target is destroyed.
As posted in the question, I currently have a script that destroys the gameObject when it is hit by my bullet, but I would like the gameObject, after it dies, to respawn somewhere else in the map. This is my script, thanks in advanced! using UnityEngine; using System.Collections;
public class DestroyTarget : MonoBehaviour {
// Use this for initialization
void OnCollisionEnter(Collision col) {
if(col.gameObject.name == "Sphere")
{
Destroy(col.gameObject);
}
}
}
Answer by allenallenallen · Feb 03, 2016 at 11:07 PM
Well, I'll tell you how I would code it in my mind but I'm not actually going to code it. That'll be your job.
First of all, delete this entire script. It's impractical without pooling and you shouldn't literally "destroy" them in the first place.
Since this object will always respawn, you only need to disable the renderer and collider components after collision.
Then you just wait for a few seconds using the IEnumerator and then re-enable the renderer and collider components after you change the position of it to a new random position.
There are thousands of questions asked regarding spawning in random areas so feel free to Google them. http://answers.unity3d.com/questions/659036/spawn-gameobjects-at-random-locations-in-limited-a.html