- Home /
Question by
eeveelution8 · Apr 03, 2014 at 12:00 AM ·
collisioncollisiondetectionenemy healthhealth-deductionhealth
A object that takes away an enemies health.
I have two scripts, one is attached to the enemy called enemyhealth, and a script attached to the projectile called changehealth. This is the changehealth script:
function OnCollisionEnter (myCollision : Collision) {
if(myCollision.gameObject.name == "enemy"){
var health : enemyhealth = myCollision.gameObject.GetComponent(enemyhealth);
health.HP = HP - 5;
}
}
and this is the enemyhealth script:
var HP : int = 5;
function Update () {
if(HP < 0) {
DestroyObject (gameObject);
}
}
The only problem is that the changehealth script cannot access the HP value of the enemyhealth script. I can't figure out how to fix it. What do I do?
Comment
Best Answer
Answer by eeveelution8 · Apr 03, 2014 at 12:08 AM
figured what went wrong.
function OnCollisionEnter (myCollision : Collision) {
if(myCollision.gameObject.name == "enemy"){
var health : enemyhealth = myCollision.gameObject.GetComponent(enemyhealth);
health.HP = health.HP - 5;
}
}
Answer by Ermarrero · Apr 03, 2014 at 12:36 AM
or you can type:
health.HP -= 5;
and instead of making a variable you can do this:
myCollision.gameObject.GetComponent(enemyhealth).health.HP -= 5;
Your answer
Follow this Question
Related Questions
How to make player keep getting hurt when touching fire? 1 Answer
Obstacle avoidance 1 Answer
Collision detection for sprite 1 Answer
How does Unity detect collision? 0 Answers