How to make a c# variabel equal a static java variabel?

I have two healthscripts in the same object (the player) and both of them have a health variabel. The first script is a javascript with a health variabel named “PlayersHealth”. The other script is a c# script with a health variabel named “health”. I want the cs. “health” variabel to be equal/ always have the same value as the js. “PlayersHealth” variabel.

To do so i have made the js. “PlayhersHealth” variabel static:

static var health : float = 100;

But i dont know mutch about c#, so what i would need is a line of code that get the js. “PlayersHealth” var (script named PlayersHealth. And a line of code telling something like: Public float health = PlayersHealth

Right now health = 100 in the c# script.

I have trided to apply what is told in the documentation but without any result:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Anyone who know something about scripting in c# and belive they can help me? :slight_smile:

The greatest problem here is how to access a JS script from C# code (and vice versa): scripts written in one language can’t be accessed by the other compiler at compile time - but once a script is compiled it becomes pure IL code, and can be accessed by any language. The solution is to make sure the JS script is compiled before the C# code: place it in Standard Assets or Plugins (or in its subfolders), and the C# script in another folder in Assets (see Script Compilation).

If the scripts are compiled in the appropriate order, you can access the JS variable like this (C# script):

void Update(){
  health = PlayersHealth.health;
}

This code will keep the C# health variable equal to the health variable that exists in the JS script PlayersHealth.js - actually, you don’t even need a C# health variable: just use PlayerHealth.health instead.

EDITED: There’s another solution that doesn’t depend on the compilation order: use SendMessage to call a C# function that updates its health variable. Add this function to the C# script:

void UpdateHealth(float h){
  health = h;
}

And call this function via SendMessage in the JS script whenever you modify the health variable - for instance:

// drag here the object to which the C# script is attached:
var cScriptOwner: GameObject; 

function ApplyDamage(damage: float){ // function where you modify health
  health -= damage; // modify health
  // call UpdateHealth in the C# script
  cScriptOwner.SendMessage("UpdateHealth", health);
  if (health < 0){
    ...
  }
}

NOTE: If both scripts are attached to the same object, you don’t need a reference to it - delete the cScriptOwner variable and remove it from SendMessage:

  SendMessage("UpdateHealht", health);

Hi, I tested the following code and it works well.

Points to note:
HealthMain.js must be placed OUTSIDE the Standard Assets or Plugins folders

HealthSecondary.cs must be placed INSIDE any subfolder in the Standard Assets Folder

Basically, the JS script gets a reference in Start() to the C# script, and thereafter when it updates the PlayersHealth variable, it also updates a PUBLIC variable Health in the C# script.

I’ve included a random number generator and Debug.Log lines in both scripts, to test this, and indeed it works.

The JavaScript Code:

#pragma strict

var PlayersHealth : float = 100;

private var healthScript : HealthSecondary; //variable to hold reference to the C# script named "HealthSecondary"

function Start () {

healthScript = GetComponent(HealthSecondary); //get the reference to the C# script

}

function Update () {

PlayersHealth = Random.Range(1.0, 100.0);

healthScript.Health = PlayersHealth;  //assign to public variable in C# script


Debug.Log("PlayersHealth: " + PlayersHealth);


}

The C# script:

using UnityEngine;
using System.Collections;

public class HealthSecondary : MonoBehaviour {

    public float Health = 0;

	
	// Update is called once per frame
	void Update () {

        Debug.Log("Health: " + Health.ToString());

	}
}

Thank you both a lot, i went for Aldo Nalettos solution and it is working perfect! Im very greatful for this help :smiley: