if the ball hits the coin the coin need to be destoyed but how? i have this but it does not work.
#pragma strict
function OnTriggerEnter (name : Collider)
{
if(name.tag == "Player")
{
Destroy(gameObject);
}
}
does it a 2d project
does this script attached to the player
it is a 2d project
attached the script to the player
Answer by jgodfrey · Feb 21, 2016 at 11:53 PM
In your script, the "gameObject" reference will refer to the object the script is attached to. So, if the script is attached to the player, that'll destroy the player. If you want to destroy the object you collided with, you want this instead:
Destroy(name.gameObject);
Answer by Ali-hatem · Feb 23, 2016 at 11:44 AM
@Hoogstra i see now you are using OnTriggerEnter wich is a 3d detection function and you are destroying the player game object not the coins which is not happening now because of the fierst problem & the coins tag is not "Player" so you need this :
function OnTriggerEnter2D(other: Collider2D)
{
// you have to tag the coins game object i am not sure if you did it
if(other.tag == "Coin")
{
Destroy(other.gameObject);
}
}
Your answer
Follow this Question
Related Questions
GameObject.Destroy(gameObject); Not Working? 0 Answers
Static Gameobjects Destroy on Reload 0 Answers
Gameobject is null for no reason 1 Answer
The bullet never destroyed!!! 2 Answers
Unable to delete instance of GameObject 0 Answers