[SOLVED] How destroying the gameObject "shredder" deletes the instances of projectile

Hello! I have one question. It is probably a simple one, but I am trying to clearly understand what’s going on.

void OnTriggerEnter2D(Collider2D col)
{
Destroy(col.gameObject);
}

What I understand is that the gameObject here is the “Shredder”, to which the script is attached. Then how destroying this is removing the instances of projectiles from the scene?

1 Like
void OnTriggerEnter2D(Collider2D col)    
    {        
        Destroy(col.gameObject);    
    }

Hi @Mirza_Rasheduzzaman,

The OnTriggerEnter2D() method, as you can see, is passed a parameter of type Collider2D named col.

col is the other collider from the other game object which has collided with this one.

When you call subsequently call the Destroy() method you aren’t passing it gameObject which would be the current game object which this script is attached you, you are passing it col.gameObject, e.g. the other colliders game object.

Does this make sense?


See also;

1 Like

Your explaination was better than mine @Rob (as usual :stuck_out_tongue: )
I was simply putting, The ball hit the wall and the wall destroyed the ball with no harm to itself.
The ball being the projectile and the wall being the shredder.

Rob’s explaination is spot on with how the trigger method works but thought i would add laymans terms :wink:

1 Like

…it’s not a competition :wink: :slight_smile:

Can often be a lot more useful… :slight_smile:

1 Like

Thanks a lot Rob for your excellent explanation. I actually put Debug.Log(gameObject.name) to see which one was being destroyed. I should have put Debug.Log(col.gameObject.name).

Thank you irresistiblejelly. I really appreciate your reply as well.

2 Likes

You are very welcome :slight_smile:

1 Like

Privacy & Terms