How can I use one variable to reference multiple identical classes?

Hi everyone. Essentially, I have two scripts that are on two different gameObjects and I want to know which script that gameObject has, so that I can apply the correct changes to the script. The changes that I want to apply are the same functions and the scripts are identical in this case, but they just have different names, which leads me to having to create an if-else chain to check what class the object has and run the same code but with a different class name.


What I mean is, that if I have a ‘Blue-Attacker’ script and a ‘Red-Attacker’ script, and I want to decrease the health of the gameobject that I chose, I have to copy-paste the same code to decrease the health value of the Blue-Attacker or the Red-Attacker, depending on what class I chose. This seems highly untidy. I would prefer if I could do something similar to this:

var targetClass;

// if target has class1
if (target.GetComponent<Class1>() != null)
{
    targetClass = Class1;
}

// else if target has class2
else if (target.GetComponent<Class2>() != null)
{
    targetClass = Class2;
}

// Do something to targetClass
targetClass.Health -= 10;

Any ideas?

Well your title essentially says the opposite of your description ^^. You said identical classes but in your description you said you have two seperate classes.

If the classes are really the same besides the name you really shouldn’t have two seperate classes to begin with. If you want to distinguish between a red and a blue attacker, you can use tags on the gameobject or use an enum on your attacker script that indicates the group / faction / kind.

If for some reasons you really need to keep them seperated (because the funcionality will diverge in the future) you can use an interface that both classes implement. You can use GetComponent with an interface as well. Of course when you have a reference of your interface type you can only access things the interface defines. Otherwise you would need to cast that reference to the actual type, there’s no way around that if you have two different classes.

Besides using an interface, if only a tiny bit of the classes are different you can use a base class Attacker and create two derived classes for red and blue. In this case you can simply use GetComponent with the base type so you have access to all shared members.

You did not give much information on your classes. Though from your description it doesn’t seem to make any sense to have two classes. A class encapsulates data (variables / fields) and functionality (methods). If this structure is the same it’s by definition the same “class”.

I think you really need to re-think and re-write your code. As it is now it doesn’t make much sense, and continuing this way will make your head explode sooner or later.

Every behavior should do a single task. If all your enemy types can receive damage you just need the same Health behavior on them. If all your enemies can attack then you just need the same Attack behaviour on them. If then you want some of them to be “blue” and some of them to be “red”, then you just need an Appearance behavior to put this info on (or simply manipulate the material they use in the render component).