The premise can’t get simpler. You have a bullet/projectile/beam that hits FOO gameObject. The bullets now needs to deal damage to that FOO gameObject by grabbing the class that manages it, FOOManager.
FOOManager implements IDamageable. But in the world there are also other types of objects that implement IDamageable as well, but they do not share a common base class, for a number of reasons, one being that i’m using a container class for each Manager class that has health and maxHealth information.
My question is, how can the bullet get the …Manager class, only knowing that the only common ground is the implementation of IDamageable?
1 Answer
1
I think you’re going about it the wrong way.
They do not need to share a common base class. As long as they all share the IDamageable interface, you should be good. Each “xxxManager” needs to implement the “take damage” or whatever function.
Call up the interface first:
IDamageable targetClass = objectThatGotHitOrWhatever.GetComponent<IDamageable>();
// check if not null, and call up damage function:
if (targetClass != null)
targetClass.TakeSomeDamage(thisMuchExactly);
By doing that, you’re not calling the interface’s template function, you’re actually calling whatever is the latest override of that function.
That’s the whole point of actually using interfaces, so that you don’t really have to know what class the target object is. You just have to know that it can take damage in some way. Each “xxxManager” can do different things in their TakeSomeDamage function, but the main point is you know that they have such a function.
Ok so change the tris array to 2,1,0,0,1,3
– Zodiarc