I have a bullet game object that detects enemies via OnTriggerEnter2D()
. This was working perfectly until I added different enemy types:
private void OnTriggerEnter2D(Collider2D collision) { Enemy enemy = collision.GetComponent<Enemy>(); if (enemy != null) { enemy.Destroy(); GameObject effect_ = Instantiate(bulletEffect, transform.position, transform.rotation); Destroy(effect_, 0.5f); Destroy(gameObject); }}
So simply if the bullet collides with Enemy()
then it should call its Destroy()
method.
The problem is that now I have added multiple enemies in my game, for example there is an enemy with the class name Runner()
with its own death method.
I can add an if condition and cycle through every enemy type but that will become tedious after more enemies are added.
Whats the best way of doing this?
Thanks
#c-sharp #unity