Ok so I have two enemies they are the same objects/models and same exact script. As you can see I gave the hands a empty with a collider to use it for a hit box. So when I play the game with just one enemy it works perfect but when I throw 2 or more. Only 1 enemy can hurt the player. here is my code
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "EnemyHitbox" && GameObject.FindGameObjectWithTag("EnemyBot").GetComponent<RobotFighter>().attacking) {
Debug.Log ("YOU BEEN HIT BY "+HitBox.BoxType+" Damage Delt: "+HitBox.Damage);
//if(HP > 0){
//}
if(!dead){
//transform.LookAt(targetPlayer);
//GETs knocked back
if(!playerSFX.isPlaying)
{
playerSFX.clip = playerClips[3];
playerSFX.Play();
}
animator.Play("HIT_BODY");
transform.Translate (-Vector3.forward*2 * Time.deltaTime);
}
//rigidbody.rotation = targetPlayer.rotation*-1;
//rigidbody.AddForce (-Vector3.forward*200,ForceMode.Acceleration);
//HP-=HitBox.Damage;
HP-=other.gameObject.GetComponent<HitBox>().damage;
}
}
I believe your problem lies with this conditional:
GameObject.FindGameObjectWithTag("EnemyBot").GetComponent<RobotFighter>().attacking
If all of your robots have the tag “EnemyBot”, your conditional will only find the first regardless of which enemy caused the collision. If your collider is attached to the enemy, then your conditional would look like this (referencing the gameobject that the collider is attached to):
gameobject.GetComponent<RobotFighter>().attacking
If you get a null reference exception when running this, then my best guess is that your collider is attached to a child of the enemy and not the enemy object itself. In this case, you’d need to reference the parent/root in your script.
gameobject.transform.parent.GetComponent<RobotFighter>().attacking
or
gameobject.transform.root.GetComponent<RobotFighter>().attacking
It’s not optimized, but it should work for you.
For future reference:
‘GameObject’ is a type, like Transform, Renderer, etc.
‘gameObject’ is a reference to an actual gameObject in the scene that is of the type GameObject.
If an analogy helps, you can reference a specific apple, which is of type ‘Fruit’, but you need to specify the actual piece of fruit you’d like to act upon… ‘Fruit’ is ambiguous.