Assigning script to variable without knowing script name

I’m creating a game where I have multiple enemy types, each with their own unique script (All enemy scripts inherit from a common script called “Enemy.cs” though (Enemy.cs inherits from Humanoid.cs, not sure if need to know but it might)).
So when a bullet with an OnTriggerEnter method comes into contact with one of these enemies, and I want to access it’s variables/methods, I can’t simply say

EnemyScript enemy = other.GetComponent<EnemyScript> ();

Because I have no idea which script it will be! Can someone help me out here? Thanks.

A way of solvin this is that you can create an “Object” variable (public) and then assign the script to it, and then with myScript.GetType() you can get the “name” of the script

I understand what this is supposed to do but I’m still a bit confused on it.
Mind giving an example?

For this situation, I typically have an integer id attached to the base for type. This way, subclasses can then assign the proper type id, and you can look at the enemy script this way.

Similarly, you can just try casting to the specific type on GetComponent, and if it returns null, then the object doesn’t have that type of script on it.

at the variables:
public Object script;
you attach the script in the inspector

in your code:
yourGameObject.GetComponent<script.GetType>();

.GetType() returns the “name” of the script (it’s “type” to be specific) and because getComponent needs a “type” to be defined, it should work

I have multiple enemy types and I will not know which one the bullet hits.

Mind going into more detail?

Nie.
wiem, że trochę się spóźniłem … ale daj mi wroga.cs metody OnTriggerEnter:)

private void OnTriggerEnter (Collider other)
{
if (other.tag == “Pocisk”)
{
// umieść tutaj co chcesz
}
}

Interfaces can help you solve this. For example, let’s pretend we want to set health variable to a bunch of unknown scripts. Declare an interface:

public interface IHealth {
  public float CurrentHealth { get; set; }
}

Implement it

public class EnemyBug : IHealth {
    public float currentHealth {get; set;}
}

and use like this

var health = other.GetComponent<IHealth>();
if(health != null) // other have some health points
{
   health.currentHealth = 100;
}

Usually you’d inverse that - your EnemyScript would have a (potentially abstract) HitByBullet method that handled getting hit. Or you’d do what @palex-nx is suggesting, which is better design.

Still, if you want to dispatch manually in the OnTriggerEnter, you can just check the type and decide based on that. This is pretty clean in newer versions of Unity with an up-to-date C# version, by using the new pattern-matching switch statement

EnemyScript enemy = other.GetComponent<EnemyScript> ();

switch (enemy) {
    case null: // aka other didn't have an EnemyScript component.
        break;
    case Goblin goblin:
        HandleHittingGoblin(goblin);
        break;
    case Dragon dragon:
        HandleHittingDragon(dragon);
        break;
    default:
        Debug.Log($"Hit unknown enemy type {enemy.GetType()}");
        break;
}

This goes against alot of patterns. SOLID for one. You have a hittable object being hit by a hitter object.