I need help with my health script

hi, i was trying to convert these scripts c # to javascript, but they are not working correctly.


playerHealth javascript:

	public var maxHealth : int = 100;
	public var curHealth : int = 100;
	public var healthBarLenght : float;
	
	// Use this for initialization
	function Start () {
		healthBarLenght = Screen.width / 2;
	
	}
	
	// Update is called once per frame
	function Update () {
		addjustCurrentHealth(0);
	}
	
	function OnGUI () {
		
		
		GUI.Button(new Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);
		
}
	function addjustCurrentHealth(adj : int) {
		curHealth += adj;
		
		if(curHealth < 0)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
			
			healthBarLenght = Screen.width / 2 * curHealth/(maxHealth);
			}

playerAttack javascript:

	 var target : GameObject;
	 var attackTimer : float;
     var coolDown : float;
	
	function Start () {
		attackTimer = 0;
    	coolDown = 0.45f;
	
	}
	
    
    function Update() {
		if(attackTimer > 0)
    	attackTimer -= Time.deltaTime;
    	
    	if(attackTimer < 0)
    	attackTimer = 0;
    	
    	if(Input.GetButton("Fire1")) {
    	if(attackTimer == 0)
    		attackTimer = coolDown;
    	
    	
		}
	}
	
	private function attack () {
		var distance : float = Vector3.Distance(target.transform.position, transform.position);
		var dir : Vector3 = (target.transform.position - transform.position).normalized;
		var direction : float = Vector3.Dot(dir, transform.forward);
		
		
		if(distance < 0.8) {
			if(direction > 0) {
    	var eh : enemyVida = (enemyVida).target.GetComponent("enemyVida");
    	eh.addjustCurrentHealth(-10);
    	}
    	}
    	}

someone could help me.

var eh : enemyVida = (enemyVida).target.GetComponent(“enemyVida”);

change it to

var eh : enemyVida = target.GetComponent(enemyVida);

otherwise this looks ok I think…

but I don’t see why you are using GUI.Button, maybe GUI.Label would be more appropriate…

GUI.Label(Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);

EDIT:

add this line:

 private function attack () {
       var distance : float = Vector3.Distance(target.transform.position, transform.position);
       Debug.Log(distance);
       ...etc

also btw, get rid of this:

function Update () {
   addjustCurrentHealth(0);
}

it’s pointless…

Make sure you are calling the attack function (it is not being called from anything here that I can see)… If the Debug.Log never shows, that’s the problem

by not working correctly is it that your health always stays at 100? Or that the script has errors.

The way it’s written now is saying curhealth = 100;

I would do var curhealth : int;
and then after function start()
curhealth = 100;

Whats the exact problem? Or, am I reading wrong?