0
\$\begingroup\$

im trying to destroy a single game object using the following script but it destroys all of them , not the ones I want. I want to destroy the ones that the transform that holds this script collides with.

It's a 2D unity3d game btw.

The script :

var keys : List.<Transform>;

function OnTriggerEnter2D(coll : Collider2D)
{
    for(var i = 0; i < keys.Count; i++)
    {
        Destroy (keys[i].gameObject);
    }
}

I hope I was clear enough , don't hesitate to ask any questions.

Thank you for your time.

\$\endgroup\$
0
\$\begingroup\$

I fixed this by editing my code into this :

function OnTriggerEnter2D(coll : Collider2D)
{
    if(coll.gameObject.tag == "Key")
        {
            Destroy (coll.gameObject);
            keysGathered ++;
        }
}

I didn't realise it was this simple.

Thank you for your time!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.