What Am I Doing Wrong

So im trying to make my player destroy a cube when he hits it I compile my code and i have no errors but it still dosen't work. Heres my code:

function OnTriggerEnter( hit : Collider ) { if(hit.gameObject.tag == "Player") { Destroy(gameObject); } }

If anyone can explain to me what i did wrong i would really appreciate it.

2 Answers

2

It's unclear from your question - the script would need to be attached to the cube(s), not the player. If so, is your player object definitely tagged as "Player"? Does the player / cube have a rigidbody? Is the IsTrigger property set to 'True'?

One suggestion I'd make for general debugging is to use Debug.Log. You could modify your code to quickly rule out a few potential issues.

function OnTriggerEnter( hit : Collider )
{
   Debug.Log("Hit was triggered");
   if(hit.gameObject.tag == "Player")
   {
      Debug.Log("Collider hit was Player");
      Destroy(gameObject);
   }
}

You can then view your log and see which messages appeared. If the second message didn't show, the problem most likely lies with the tagging, or that the script is on the player and checking for the "Player" tag on the cubes.

Let me know how you go.

Thanks! From what i have read so far it seem like once you add a script to an object it trumps all physics made by unity.