- Home /
Instantiate after collision
Hi, for some reason my code wont Instantiate a new gameobject after I hit it. Can someone help?
var scriptName : GameObject;
var target: GameObject;
function OnCollisionEnter(theCollision : Collision){
// collision for ball destroys object
if(theCollision.gameObject.name == "BasketBall(Clone)")
{
scriptName.GetComponent(Jh_Score_Script).addsometoscore();
Destroy(gameObject);
yield WaitForSeconds(1);
Instantiate (target, Vector3(0,1,0), Quaternion.identity);
}
}
Does the object you have this script attached to have a rigidbody component?
I have attached to a an empty gameobject and put a target prefab as the gameobject variable. I as you can tell i'm trying to get one target to destroy on collision and instantiate another.
Try doing this:
gameObject.renderer.enabled = false;
yield WaitForSeconds(1);
Instantiate (target, Vector3(0,1,0), Quaternion.identity);
Destroy(gameObject);
OnCollisionEnter only works if there is a rigidbody attached. See Documentation
Answer by Doireth · Oct 15, 2013 at 10:40 AM
For collision to occur your object needs a Collider and a Rigidbody. Without both of those "OnCollisionEnter" never triggers.
You use Destroy()
meaning that the object is destroyed after the current Update loop, so code delayed with a whole second will never be run, because the object and the component no longer exists then.
I have this. The collision occurs on both instances. The script now instantiates one target and then still detects collision but will not destroy or instantiate another target.
var scriptName : GameObject;
var target: GameObject;
function OnCollisionEnter(theCollision : Collision){
Debug.Log("Have I hit anything");
// collision for ball destroys object
if(theCollision.gameObject.name == "BasketBall(Clone)")
{
gameObject.renderer.enabled = false;
Instantiate (target, this.transform.position, Quaternion.identity);
Destroy(gameObject);
scriptName.GetComponent(Jh_Score_Script).addsometoscore();
Debug.Log("Have I hit basketball");
yield WaitForSeconds(3);
}
}
This method just did not work so i made a trigger script.
// plug in score script
var scriptName : GameObject;
// plug in tartget to instantiate
var target : GameObject;
//plug in audio clip
var explosion : AudioClip;
// sets int for basket
var wentThroughBasket = 0;
function OnTriggerEnter (myTrigger : Collider)
{
// checks for collision with basket ball
if(myTrigger.gameObject.name == "BasketBall(Clone)")
{
scriptName.GetComponent(Jh_Score_Script).addsometoscore();
wentThroughBasket++;
var targets : GameObject = Instantiate(target,Vector3(-88,6,1), Quaternion.identity);
if(wentThroughBasket == 3)
{
WaitForSeconds (4);
Application.LoadLevel(5);
}
}
}
I then attached a collision script to the prefab.
// variable for sound var explosion : AudioClip;
// Collision Detection function OnCollisionEnter(theCollision : Collision){
// collision for ball destroys object if(theCollision.gameObject.name == "BasketBall(Clone)") { // destorys target Destroy(gameObject); }
if(theCollision.gameObject.name == "Target")
{
//plays audio
audio.PlayOneShot(explosion);
}
}
Answer by Jamie_h88 · Oct 15, 2013 at 10:09 AM
I have changed the Destroy command to Destroy(target), would this not be away around this?
Please post comments by clicking Add new comment under my answer ins$$anonymous$$d of posting a new answer. :) That depends on what you're trying to destroy. You chould do this if you just want the Game Object to disappear on collision:
gameObject.renderer.enabled = false;
yield WaitForSeconds(1);
Instantiate (target, Vector3(0,1,0), Quaternion.identity);
Destroy(gameObject);
Is the collision code never run?! Do you have a Rigid Body component on the Game Object this script is on? Are you sure the object you're colliding with is called "BasketBall(Clone)"?
Try adding Debug.Log(theCollision.gameObject.name);
after function OnCollisionEnter(theCollision : Collision){
to see if the name of the Game Object fits with the name in the if conditional.
Your answer
Follow this Question
Related Questions
Having problems with destroying, instantiating 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers
Collision Detection for a Prefab? 3 Answers
Instantiate and Destroy an object while crossing a line with its clone also 1 Answer