- Home /
After Destroy clone, Spawn a prefab in its location?
How would you spawn a prefab after the clone was destroyed in its place. When the asteroid hits Jupiter I would like the have a explosion effect after the collision. Here's my script.
using UnityEngine;
using System.Collections;
public class CollisionDetecter : MonoBehaviour {
public GameObject explosion;
void OnTriggerEnter(Collider asteroid) {
var spawnSpot = asteroid.transform.localPosition;
Destroy(asteroid.gameObject);
print ("destroyed " + asteroid + " " + asteroid.transform.localPosition + spawnSpot);
GameObject newObj = Instantiate(explosion, spawnSpot, Quaternion.identity) as GameObject;
newObj.transform.parent=transform;
newObj.tag = "Clone";
}
}
Currently the Explosions spawns somewhere else, which is way off. I tried to get the position of the asteroid, I also put explosion inside the sphere, still the same.
Here is my Hierarchy. The explosion is position 6 inside sphere. Maybe its something easy like asteroid.transform=transform, like with the parenting.
Answer by cowasockytommy · Oct 04, 2013 at 04:30 PM
i would instantiate b4 destroying try that. and also i'd use var spawnSpot = asteroid.transform.position;
What do you mean by instantiate? But anyways the asteroid.transform.position; has hit the spot, thx a lot.
GameObject newObj = Instantiate(explosion, spawnSpot, Quaternion.identity) as GameObject;
here your instantiating... most times its better to instantiate b4 u destroy objects that require positions. but glad it helped
Your answer
![](https://koobas.hobune.stream/wayback/20220613115317im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
I want to change the object position random in five particular position only. 0 Answers
Syncing object's coordinate position with object's animation position. 3 Answers
Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers
How do i move a GameObject to the same height as another GameObject? 0 Answers