Problem with Destroy(this.gameObject);

Well,

Hello guys.

I’m starting with coding by my self in unity 3D.
I made script which should delete coin after colliding with Chat (player)

The script is:

#pragma strict

function Start () {

}

function Update () {

}
function OnTiggerEnter (nesne:Collider) {
if (nesne.collider.name=="Char")
Destroy(this.gameObject);
}

So yea. I save it, I don’t get any errors.
I assigns a code to coin.

But when I try to catch coin nothing happens. Coin doesn’t disappear.
:face_with_spiral_eyes:
Here is small SS.

http://scr.hu/0r27/gy7y8

Can somebody help me?

the coin needs a collider on it.

Thx for answer, Ok, I forgot about it… But is still doesn’t work.

http://scr.hu/0r27/eehga
Any ideas?

anybody ?

has the other object got a collider on it?
Has one of them got a rigidbody?

Yea, I’m using First Person controller so he have collider as well.Coin have also collider.

is the name of the thing correct?

You have Char in the script but say Chat in the post

who is this tigger you speak of?

OnTiggerEnter

OnTriggerEnter

also, collider needs to be set to being a trigger

I’ve never used “this.gameObject” as a parameter for the “Destroy” function before, I just use “gameObject” and it works all the time if my colliders are all set up properly. Check all your colliders and put rigidbodies on them, also if your using “OnTriggerEnter” in the coin’s code, you must make the players collider “Chat/Char” a trigger. I recommend switching this so the coin is the trigger because if your players collider is a trigger he’ll be able to walk through walls like a ghost (boo!).

Also I found a very weird “glitch”, it sometimes happens if one collider in a collision is NOT moving, the collision won’t be detected. For example the coin is probably stationary therefore the collision won’t be detected. To overcome this is real easy it just takes 2 lines of code

So try this code on your coin and check all your colliders :

function Update ()
{
//to overcome the hit detection glitch
transform.Translate(0, .01, 0);
transform.Translate(0, -.01, 0);
}

function OnTriggerEnter (nesne : Collider)
{
if(nesne.gameObject.name == "Char")
{
Destroy (gameObject);
}
}

That chunk of code would go onto your coin. Best of luck with the game!