Other proposal to destroy the projectiles

Hi,

Instead of adding a script to the GameOject Schredder, I created a script attached to the laser projectile, then I used the same method to destroy the projectile.

private void OnTriggerEnter2D(Collider2D collision)
{
Destroy(gameObject);
}

This work fine.

Is solution equivalent ? which one is the best ?
Thx

Either method works fine, but it’s usually good to be consistent in where the OnTrigger methods are. For example: If I remember correctly (it’s been a while since I did the Laser Defender course, the enemies have an OnTriggerEnter2D as well, where they respond to the hit and destroy the projectile.
You want to be able to always know who is responsible for destroying an object in the event of a collision/trigger. It might not seem to matter in a small project, but suppose you go back to that project and begin adding lots of features… Say a force field that causes the projectile to bounce back at the shooter… Your force field detects the trigger enter and reverses transform.rotation, but… it dissappears anyways… you might have forgotten that the projectile script destroys itself and spend hours working out this strange bug in your force field code.
That being said, at this stage in the journey, either method works fine.

1 Like

Privacy & Terms