- Home /
Enemy health
Hey, i have a problem with my enemy script. i didn't found the solution in similar topics. I have 2 enemies (dublicated). My problem is, that the life of both is decreasing simultaneously. Do you know why?
1. Enemyscript
public static var Health = 100;
static function Damage(){
if(Health > 0){
Health -= Cannon.myDamage;
Debug.Log(Health);
}
}
2. Cannonscript
var mySpeed : float = 10; var myRange : float = 10; public static var myDamage : float = 5; private var myDist : float; var counter = 1;
static var testname;
function Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
myDist += Time.deltaTime * mySpeed;
if(myDist >= myRange){
Destroy(gameObject);
}
}
function OnCollisionEnter(collision: Collision)
{
if(collision.gameObject.tag == "Enemy")
{
if(collision.gameObject.name == "Enemy!"){
if(collision.gameObject.GetComponent(CubeEnemy).Health > 0){
collision.gameObject.GetComponent(CubeEnemy).Damage();}
if(collision.gameObject.GetComponent(CubeEnemy).Health <= 0){
Destroy (collision.gameObject);
}
Answer by Piflik · Aug 29, 2012 at 01:00 AM
Because your health variable is static. There is only ever one instance of a static variablein the scene, and when two or more objects use it, they are always both affected at the same time. Use normal, public variables for your enemies' health instead. Also rhe static nature of the function might give you problems.
Use static variables/functions only, if they are used uniquely, for example the player's health.
Philipp's right, you've made the health variable global, which means that all gameobjects in the scene can access it, and both of these objects ARE accessing it, so they can both make changes to it that will reflect on the other. What you want to do is make it a local variable so that it only affects (and can be easily affected by) the gameobject it belongs to.
So, you would want to change:
"public static var Health = 100;"
to
"public int Health = 100;"
Then, each gameobject with this script would have it's own "Health" attribute.
Answer by Dasherz · Aug 29, 2012 at 12:55 AM
if they both have the same health variable "health" then both their health is going to go down at the same time.
make 2 different variables
enemyHealthOne enemyHealthTwo
also within your tag you will have to define which enemy you are hitting.
eg
if(collision.gameObject.tag == "Enemy")
{
if(collision.gameObject.name == "enemyOne"){
enemyHealthOne --;
......
else if(collision.gameObject.tag == "Enemy")
{
if(collision.gameObject.name == "enemyTwo"){
enemyHealthTwo --;
......
Hope this helps
While this might work for two enemies, it will get extremely complicated with multiple enemies and is not practicable. Gladly it is also completely unnecessary, because each instance of an object uses its own set of variables, as long as they are not static.
Answer by Sundar · Aug 29, 2012 at 12:56 AM
try this
static function Damage(){
if(this.Health > 0){
this.Health -= Cannon.myDamage;
Debug.Log(this.Health);
}
Don't use statc functions when you want to access non-static data (which Health isn't, but you treat it like it was). It won't work.
Also this.Health will not produce anything usefull, because Health is still static and static variables aren't attributable to any object.
Answer by Vmadmax · Aug 29, 2012 at 10:38 AM
Thank you very much! I now understand what I did wrong. I works perfectly