I ran into a bit of a problem. Basically I want to make lot’s of different enemies all of which would have their separate AI script. Since you can’t name multiple script the same name, so I would just name them for example SlimeAI, WarriorAI, etc. And the way I have my attack system set up now I need to provide a specific name of the script, which won’t allow me to have different AI scripts for different enemies. The only way I can think of right now is to use if/case statement, but that would get messy when I have a lot of enemies. Is there any easier and efficient way of solving this problem?
private void OnCollisionEnter2D(Collision2D collision) {
//Do different things based on the layer of the object the player collides with
switch (collision.gameObject.layer) {
case 10:
Attack(collision);
break;
default:
break;
}
}
private void Attack(Collision2D collisionAttack) {
collisionAttack.gameObject.GetComponent<SlimeAI>().health -= totalAttackDamage;
}
public class BaseAI : MonoBehaviour {
public virtual void Somefunction() {
Debug.Log("Base");
}
}
// different script file
public class SlimeAI : BaseAI {
public override void Somefunction() {
Debug.Log("Slime");
}
}
//your collision function
private void OnCollisionEnter2D(Collision2D collision) {
BaseAI ai = collision.gameObject.GetComponent<BaseAI>();
if (ai != null) {
ai.Somefunction();
}
}
Because SlimeAI is derived from BaseAI, it has the “Somefunction” function - but it can override that function’s code with its own. And GetComponent will also find any class that is derived from BaseAI.
Another option, though the previous post could be more what you want, is to have a Health script of some sort that all enemies have (could include whatever you find appropriate). Each instance of Health may have different stats, but you could access it by its component on any AI , regardless.
Thanks! It solved the problem, but if you can, could you explain a bit more how collision.gameObject.GetComponent(); gets the right class based on the enemy, cause now I’m not really sure that I understand that part clearly, because I understand that it would just get the BaseAI class, not the class that derived from it.
It gets whatever happens to be attached to the object that you collided with. If there’s a SlimeAI attached (and SlimeAI derives from BaseAI), then it gets you the SlimeAI.
Note that even if the object it’s pointing to is a SlimeAI, the type of the variable (ai) is still BaseAI. So if you have additional functions on SlimeAI that don’t exist on BaseAI, then you won’t be able to call them unless you typecast it:
SlimeAI slime = ai as BaseAI;
if (slime != null) {
slime.StickToPlayer();
}
Now that you know that, you should beware that this is considered bad coding practice, because you’ll end up with a bunch of “special exceptions” (which inheritance is specifically intended to avoid). The correct way to do that would be to have a virtual, more generalized function in BaseAI (say, public virtual void ReactToPlayerContact), and then override that function in SlimeAI so that it calls StickToPlayer on its own.
Thanks for the suggestion, but yeah I went with the other solution because in the future there will be more stats and functions that different enemies share and the first solution would take care of that.