How do i access a variable in one script from another script?

Hey, I am trying to access a variable in a script called HealthBarScript. HealthBarScript is attached to a gameObject called Player, whilst i want to access in a script called EnemyMovementScript wich is on a gameObject called Enemy. Here are the scripts.

Here is the HeathbarScript

using UnityEngine;
using System.Collections;


public class HealthBarScript : MonoBehaviour {
	
	public int Maximumhealth = 100;
	public int CurrentHealth = 100;
	
	public float healthBarLength;
	
	// Use this for initialization
	void Start () {
		healthBarLength = Screen.width / 2;
	}
	
	// Update is called once per frame
	void Update () {
		AdjustCurrentHealth(0);
	}
	
	void OnGUI(){
		GUI.Box(new Rect(10, 10, healthBarLength, 20),
		        CurrentHealth + "/" + Maximumhealth);	
	}
	
	public void AdjustCurrentHealth(int adj){
		CurrentHealth += adj;
		
		if(CurrentHealth < 0){
			CurrentHealth = 0;	
		}
		
		if(CurrentHealth > 100)
		{
			CurrentHealth = Maximumhealth;	
		}
		
		if(Maximumhealth < 1){
			Maximumhealth = 1;	
		}
		
		healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
	}
}

And here is the MovementScript

var target : Transform; //the enemy's target

var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy

function Awake()

{

    myTransform = transform; //cache transform data for easy access/preformance

}



function Start()

{

    target = GameObject.FindWithTag("Player").transform; //target the player

}



function Update () {

    //rotate to look at the player

    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);



    //move towards the player

    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

function OnCollisionEnter(theCollision : Collision){

 if(theCollision.gameObject.name == "Player"){

 }else if(theCollision.gameObject.name == "Golv"){

  	Debug.Log("Hit the wall");
 }
}

I want to access CurrentHealth in the EnemyMovementScript at the bottom function called OnCollisionEnter. How would i do that?

What i want to happend is that i want them to collide and the health si withdrawn by like 5 or something.

Like @syclamoth said, the problem here is the different languages: JS and CS can’t see each other during compile time. There are a few ways to solve this, but since you need only a one-way communication path, I think the best alternative is to use SendMessage to call a function in the CS script that modifies the health.

In the CS script:

void AddToHealth(int value){ // add value to the health
    CurrentHealth += value;  // (or subtract, if the value is negative)
}

If both scripts are attached to different objects, you must have in the JS script a reference to the CS object - transform, gameObject, collider etc.

JS script:

var healthObject: Transform; // drag the health bar object here

function DecreaseHealth(){ // this function reduce health by 5
    healthObject.SendMessage("AddToHealth", -5);
}

The other alternatives are:

1- Compiled scripts are visible to any language: if you compile JS after CS, the JS script will be able to “see” the CS one - but not the other way around, thus you may have problems with the JS script being invisible even to other JS code already compiled. The compilation order is explained in Script Compilation.

2- Rewrite the CS script in JS (or vice versa, if you have more CS than JS scripts). Maybe someday Unity will be able to compile JS, CS and Boo with the same compiler, but until that the best solution is to have all scripts written in the same language.

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html