PlayerHealth

0
i wont my ai reduce the player health

i try this script but thers not working

var cube : GameObject;

function Awake() { cube = gameObject.FindWithTag("Player"); }

function OnCollisionEnter(hit : Collision) { if(hit.gameObject.tag == "cube") PlayerHealth.curHealth -= 20; }

===================================================

this is the AI FOLLoW script

var target : Transform; var moveSpeed = 20; var rotationSpeed = 5; var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() {

target = GameObject.FindWithTag("Player").transform;

}

function Update () {

myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); 
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

function PlayerHealth () {

PlayerHealth.curHealth -= 20;

}

=====================================================================================

and this is the PlayerHealth script

static var curHealth : int = 100; static var maxHealth : int = 100;

var healthtext : GUIText;

static var curXp : int = 0; static var maxXp : int = 500;

var xptext : GUIText;

var level :int = 1;

static var minAttack : int = 10; static var maxAttack : int = 50;

static var attackPower : int = 2;

function Start () {

healthRegen();

}

function Update () {

healthtext.text = curHealth + " / " + maxHealth;

xptext.text = "Level " + level + " xp " + curXp + " / " +maxXp;

if(curXp == maxXp) {

levelUpSystem();

}

if(curHealth < 0 ) {

curHealth = 0;

}

if(curHealth > maxHealth) {

curHealth = maxHealth;

}

if(Input.GetKeyDown("e")) {

curHealth -= 10;

}

if(Input.GetKeyDown("r")) {

curXp += 10;

}

}

function healthRegen () {

for(i=1;i>0;i++) {

yield WaitForSeconds(0.5);

if(curHealth < maxHealth) {

curHealth++;

}

}

}

function levelUpSystem () {

curXp = 0; maxXp = maxXp + 50; level++;

maxHealth += 50; Inventory.inventoryArray[0] += 1; Inventory.inventoryArray[1] += 1;

}

anyone can healp me??? I realy need this

Please put notes behind ‘//’ :slight_smile:

Ok, static variables only allow one instance so using them for enemies is a bad idea because when you take the health down on one, the others will take effect too. Instead, use your own functions:

function decreaseHealth(decrease : int) {
     health -= decrease;
}

Can you elaborate more on what you’re trying to do?