How to destroy cube when collision occur

hello im new in unity,please help me… i have three object,one is for life,one for player and another one is as a enemy,when the player hit the enemy,i want to destroy object that is player life…all object i using cube…i made some js scipt

function OnCollisionEnter(col : Collision)
{
           
            Destroy(gameObject.FindWithTag('life'));
            
}

i attached this script into enemy,is it true??

i want to destroy another cube called it "life"..i already try using your code,but it destroy the enemy,what i want to destroy life cube..and i try using this "Destroy(GameObject.FindGameObjectWithTag("life"));"......but this error come out.."UnityException: Tag: life is not defined! collision.OnCollisionEnter (UnityEngine.Collision col) (at Assets/collision.js:8)"

In about 99.9% of the cases the answer of an error is in the error. You are part of those 99.9%. " life is not defined" You have not defined life in the tag list nor have you assigned it to the object. Since it does not exist. Again, you cannot just create things on the fly. Or make sure it is not Life.

sorry,, i dont know how to defined it.. what u mean in the tag list??? can u give some example???

http://answers.unity3d.com/questions/36212/how-do-i-add-a-new-tag-in-unity.html

now i now where is tag lies...but,how can i choose those tag,it dont give me an option to choose it...???

1 Answer

1

What are you trying to destroy? Id it is the object you are colliding with use the col reference:

function OnCollisionEnter(col : Collision)
{ 
     if(col.CompareTag("life"))
         Destroy(col.gameObject);
}

If you need to find another object then you need:

Destroy(GameObject.FindGameObjectWithTag(“life”));

I recommend you to use the method from Unity. You cannot just create them on the fly.

once again, if i have 3 life, how can i destroy one by one using those tag??? assume that those 3 cube life have the same name called "life"...