- Home /
When I kill one enemy, they all die?
Hello, I am making an FPS where tigers chase you and you have to shoot them. I attached a script called PaintballDamage to my "paintball" which is the projectile that comes from my gun. It looks like this: function OnCollisionEnter (theCollision : Collision){
if (theCollision.gameObject.name == "Tiger" || theCollision.gameObject.name == "Tiger(Clone)"){
TigerLife.HEALTH -= 100;
}
} My Tiger has a script called TigerLife attached to it that looks like this: static var HEALTH : int = 100;
function Update () {
if (HEALTH < 1){
Destroy (gameObject); } } I tried changing the HEALTH variable to public instead of static, but I get this error message: Assets/Scripts/PaintBallDamage.js(3,27): BCE0020: An instance of type 'TigerLife' is required to access non static member 'HEALTH'. Whenever I kill one tiger, they all die. Does anyone know how to fix this? Thanks so much!!!
Answer by whydoidoit · Jul 27, 2012 at 07:42 PM
You need to make Tiger.HEALTH (why is it all in captials?) not static. You need one for each Tiger. You would then do the following in your if statement:
theCollision.gameObject.GetComponent(TigerLife).HEALTH -= 100;
Thanks a lot, just one question: What do you mean by I need one for each tiger? Thanks so much for the help!
You need a separate value of HEALTH for each tiger - if you make it static you have one value for ALL tigers.
I converted your answer to a comment - you should hit the Add New Comment button hidden on the right when not providing a solution.
Your answer
Follow this Question
Related Questions
OnCollisionEnter not working 1 Answer
Unity Collison Problem 1 Answer
Gun Fire, sparks on Collision 1 Answer
Rayshoot Reload Problem (Can still shoot) 2 Answers
FPS Shoooting Problem 1 Answer