- Home /
Destroy on collision
I am making a game, that if a car collide with a coin, the coin will disappear. but I have no idea what script i need... so please make a reply and give it!
Answer by Montraydavis · Nov 01, 2012 at 04:26 PM
Attach this to the coin:
function OnCollisionEnter ( )
{
GameObject.Destroy ( gameObject ) ;
}
Also, be sure to add a Rigidbody and Box Collider. The object colliding into the coin also needs a rigidbody, and box collider. (PS: Do not attach this script to the player lOl. Bad results ;) )
Would probably be better making the coin a trigger ins$$anonymous$$d of giving it a Rigidbody wouldn't it?
Answer by tomsk · Sep 02, 2014 at 06:17 PM
Try this :)
public void OnCollisionEnter(Collision node){
if(node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
}
}
or this
public void OnTriggerEnter(Collider node) {
if(node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
}
}
Do you want to collect the coins and keep track of how many the player has?
this must work (i tried it):
public void OnCollisionEnter(Collision node)
{
if(node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
global.score++; //optional, if you have global with score
}
}
Dont forget to add "Coin" tag to your object
it doesn't work it just give me these errors Assets/Coin/CoinCollision.js(1,44): BCE0043: Unexpected token: ). Assets/Coin/CoinCollision.js(1,40): BCE0044: expecting ), found 'node'. Assets/Coin/CoinCollision.js(1,8): BCE0043: Unexpected token: void.
Answer by reddead12 · Jul 24, 2015 at 07:30 PM
In Javascript in would be:
function OnTriggerEnter(node : Collider){
if(node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
}
}
Or
function OnCollisionEnter(node : Collision){
if(node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to destroy bullet on collision! 2 Answers
On Collide Destroy Game Object 2 Answers
Destroy cube on collision 1 Answer