how to make the player damage one enemy

i have a simple script that when it collides with a gameobject it loses health but the problem is that every enemy in the game with the same script losses health. i know why this is happening but i dont know how to fix it, any helppp!!

static var health:float = 10;
var damage1:float=3;
var damage2:float=1;
var damage3:float=15;
var damage4:float=1000;
var takedamage=true;
var afterobject:Transform;

function OnCollisionEnter(other : Collision) {
    if (other.gameObject.tag == "bullet"){
        if(takedamage){
        //im sure the problem is here i tried puting gameObject.health but that didnt work
            health-=damage1;
             }
    }

    else if (other.gameObject.tag == "bullet2"){
        if(takedamage){
        health-=damage2;
          }
    }

    else if (other.gameObject.tag == "bullet3"){
        if(takedamage){
            health-=damage3;
      }
    }

    else if (other.gameObject.tag == "bullet4"){
        if(takedamage){
            health-=damage4;
      }
    }
}

function Update(){

   if (health <= 0){
    if(takedamage){
        var bullit = Instantiate(afterobject, gameObject.transform.position,  Quaternion.identity);
        CPH.exp +=100/CPH.level;
        monsterspawn.total -=1;
        takedamage=false;
        DestroyObject (gameObject);
        }
    }

}

change your static var for health to a public var and give it another try. If that doesn't work then can you please properly format all of your code so it's easier to read. Assuming it's all one script.

–

1 Answer

1

Thank you to Justin for formatting the code (assuming it was you sorry if it wasn't). Like i mentioned in my comment if you change your static variable into a public variable it should fix your problem. These lines here:

var afterobject:Transform;

var bullit = Instantiate(afterobject, gameObject.transform.position,  Quaternion.identity);

I think should be

var afterobject : GameObject;

Instantiate (afterobject, tranform.position, tranform rotation);

sorry if your trying to use the other method in a particular way but for a "death" object that isn't doing anything (or atleast your not trying to do anything to it with this script) doesn't need the var bullit bit and I'm pretty sure it should be a gameObject instead of a Transform. If I'm wrong then I apologize but my script that's doing this exact same thing is working just fine and those are the differences I've noticed.

gameObject.transform is the same as just transform. Quaternion.identity means the new spawn is rotated "normal," as if you dragged it in. transform.rotation means the new spawn is rotated the same as you are. If afterObject is a death effect, either is fine (but why is is named bullit?)

–

Cool thanks for clearing that up like I said I was unsure but mine works fine like this so I thought I'd give him something reliable.

–