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.
Here is small SS.
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!