- Home /
Instatiate object after collision detect
Hi, I'm new to Unity and I'm trying to create a game where a basketball is shot at a target, and if it hits the target it disappears and respawns. I've got the script to make it disappear, but I now want to appear again after the collision. Here is my script so far:
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "Target"){
Debug.Log("Score!");
Destroy(gameObject, 1);
}
}
The basketball is stored in a prefab so I just need a way of making the prefab instantiate the basketball again. Can anyone help?
Answer by hotemup · Jan 05, 2012 at 08:46 PM
Instead of destroying it you can just use active.
e.g.
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "Target"){
Debug.Log("Score!");
gameObject.active=false; }}
Then when you want it back, you can simply use gameObject.active=true;
That doesn't really help, as it just makes the object appear in the same place and I want it to reset to the original position, but thanks anyway.
gameObject.transform.position = GameObject.Find("SpawnPoint").transform.position;
Your answer
![](https://koobas.hobune.stream/wayback/20220613062712im_/https://answers.unity.com/themes/thub/images/avi.jpg)