I have a script for attacking, and it checks to see if the object has the "Enemy script" like so:
// Damage them
foreach (Collider2D enemy in hitEnemies)
{
// Debug log showing the attack connected
Debug.Log("We hit " + enemy.name);`
// the enemy takes damage from the player, could be rewritten
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
Now the issue is, it only checks to see if they object has that script. I need to also make it check for a boss script that has the load area method in it.
I can have it be only a Enemy script or the EnemyLoadArea script. The idea is, that only bosses have the EnemyLoadArea script. If I change it from "enemy.GetComponent" to "enemy.GetComponent", Unity no longer recognizes the damage, and the enemy doesn't die.
There may be a better way to have a scene load on a boss death, but I don't know if there is.
I can't leave fields blank in Unity, so I can't have the load area code in the enemy script. How can I get it to check for two different gameobjects? Or even, how can I rewrite the code to check for a tag in Unity? So instead of finding the Enemy script, it checks to see if the NPC is tagged as an Enemy, and then take damage?
I've tried using
enemy.GetComponent<Enemy || EnemyLoadArea>().TakeDamage(attackDamage);
and
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
enemy.GetComponent<EnemyLoadArea>().TakeDamage(attackDamage);
and also tried reusing the same foreach method with the component being changed from "" to <"EnemyLoadArea"> but it doesn't work either.
Every different fix I've tried return in errors like "|| cannot be used", or just
NullReferenceException: Object reference not set to an instance of an object PlayerAttack.Attack () (at Assets/Scripts/Player/PlayerAttack.cs:53) PlayerAttack.Update () (at Assets/Scripts/Player/PlayerAttack.cs:30)