Import var in another script

Hi, here is my problem,

I have a script on my character with the variable “vie” (life of the character), I want to import this variable on my ennemy script to change the damage of my enemy.

Here’s my character script

  var vie = 100;
     
    function OnCollisionEnter (hit : Collision)
    {
    Debug.Log (vie);
    if(hit.gameObject.tag == "Enemy")
    {
    vie -= 10;
    Debug.Log (vie);
    if (vie <= 0){
    Destroy(gameObject);
    }
    }
    }

here is my enemy script

var speed = 10;
var joueur : GameObject;
var vie:vieperso = GetComponent(vieperso);
    
function Update ()
{
   var distance = Vector3.Distance (joueur.transform.position, this.transform.position);
   if (joueur != null) 
  {
     if (distance < 2)
       {
           
           if (distance < 7)
              {
                 Debug.Log (vie);
                 vieperso.vie -= 20;
              }




       }
     else
       {
        this.transform.position += (this.joueur.transform.position - this.transform.position).normalized * this.speed * Time.deltaTime;
       }
  }    
   else
  {
    
  }
}

But I have this error An instance of type ‘vieperso’ is required to access non static member ‘vie’.

thx for anyhelp

Well there are a couple things you can do. The first thing you can do is make var vie a static varibale

static var vie = 100;

Then in your enemy script, add in a variable as such:

var playerScript: character;

replace “character” with the name of your character script.
Then to acess vie, place the character game object on the variable player script. You can now acess vie in the enemy script by typing playerScript.vie. Example:

if(Input.GetKeyDown("x")){
      playerScript.vie += 5;
}

The second option is slightly more scripting based.
To begin, give your player object a unique tag (tags can be located under the objects name in the inspector view). In your enemy script create a transform variable:

var player : GameObject;

Leave it blank. And add the following in the Awake function, or some function in your enemy script that only runs once:

player = GameObject.FindWithTag(Insert player tag here)

Now, with your defined player object, you can manipulate any script. So create a variable to make this easier:

var charScript;

Then define that variable:

charScript = player.GetComponent(character script name);

now you can access vie like this:

charScript.vie += 5;