Most effective way to get the methods of any collider object in Unity?

Most effective way to get the methods of any collider object in Unity?

I have a bullet game object that detects enemies via&nbsp;<code>OnTriggerEnter2D()</code>. This was working perfectly until I added different enemy types:

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five. 아시죠? C는 C++.C# 줄인말이 아니라는거?

Learn how to perform CRUD operations using C Sharp

QuickStart: CRUD Operations in C Sharp. Learn how to perform CRUD operations using C Sharp for MongoDB databases.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.