- Home /
How to detect which enemy is hit?
public class BasicWeapon : MonoBehaviour {
EnemyHealthScript enemyStats;
GameObject enemy;
PlayerMovements myPlayer;
public float damage = 12f;
public bool enemyHit;
void Awake()
{
myPlayer = GetComponentInParent<PlayerMovements> ();
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Enemy") {
enemyHit = true;
print ("Hit");
enemyStats = enemy.GetComponent <EnemyHealthScript> ();
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "Enemy") {
enemyHit = false;
print ("Lost");
}
}
void Update()
{
if (myPlayer.isAttacking && enemyHit) {
enemyStats.EnemyTakeDamage (12);
}
}
}
so thats the code. However, I dont know how to make 'enemyStats.EnemyTakeDamage()' affect the enemy that I just hit. I looked up solutions from google but either they dont know it as well or Im just not asking the right questions. Perhaps you guys can help.
"I looked up solutions from google but either they dont know it as well"
if so, we would still live in 2005, but it's not 2005, is it?
"or Im just not asking the right questions"
Ahh yeah, much better explanation.
So, it's 3d game I guess, this script is for weapon, where do you shoot some bullets or ray? How does should it work?
You can see most often something like this: shoot as a RAYCAST, get component from "hit" and here you are.
so, its a 3d game (yes) which is melee combat based. this script is connected to the melee weapon (a spear) . What I was meant to do was when the spear collides with an enemy, it will check if the character is attacking. if it is, then the enemy will take damage. if not, it just means that the weapon was colliding with the enemy, therefore, no damage will be dealt.
Answer by QuantumCookies · Apr 13, 2018 at 10:58 AM
you can do this
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
enemyHit = true;
other.gameObject.GetComponent<EnemyHealthScript>().EnemyTakeDamage();
}
}
Your getting the script attached to the enemy game object and then the enemy will take damage from that script.
hey man. your code worked. thanks for the quick reply. I couldnt continue with my work unless i solved that problem. thanks again
Answer by tormentoarmagedoom · Apr 13, 2018 at 11:03 AM
Good day.
Under the line
enemyStats = enemy.GetComponent <EnemyHealthScript> ();
You only need to:
enemyStats.EnemyTakeDamage();
But, the EnemyTakeDamage() in the enemy script must be public to access it from other script. So in the enemy script:
public void EnemyTakeDamage()
{
(Take Damage code)
}
Thas all. You should be able to do it!
IF don't, post whats hapening, you get some error? Or nothing happens? or What?
IF worked, accept the answer and close the question!
Bye!
hey man. so I already have that made. however, i dont think its able to detect which of the enemyHealthScripts it should do it to. (there are multiple enemies)
i didnt get it at first but then when I compared it to another answer, I realized what you were talking about. thanks man
Your answer
![](https://koobas.hobune.stream/wayback/20220612160649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Exceptions to tag references? 1 Answer
Help With Colliders 3 Answers
FindGameObjectsWithTag works only to first tagged 1 Answer
Changing tag via scripting.... 1 Answer
Finding children of a gameobject with a certain tag 2 Answers