- Home /
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? :)
You say that you have tried to apply what is in the documentation. What is your code like for doing this?
Answer by aldonaletto · Oct 28, 2012 at 08:46 PM
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);
Thank you!!!!
monej0005: I didnt got any error message while running, but if if the java healthvalue was changing during play the c# value did not. I Guess might i would need the write something withing void aswell.
Aldo Naletto: Great. But i can not play the code cause i get error message cs0103 telling me that the name "PlayersHealth" does not exist in the context.
Here is the C# script:
`using UnityEngine; using System.Collections;
public class HealthControllerExample : $$anonymous$$onoBehaviour {
public string hitAnimation = "";
public float health =PlayerHealth.health; //public float health = 90; void Start() { if (hitAnimation != "") { animation[hitAnimation].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once; animation[hitAnimation].layer = 3; animation.Stop(hitAnimation);
} }
protected void OnCollisionEnter(Collision collision)
{
ApplyDamage(10);
}
public void ApplyDamage(float damage) {
GameObject.Find( "PlayerHealth"); Send$$anonymous$$essage("Traffad"); health -= damage; if (health <= 1) if (health >= -10){ GameObject.Find( "PlayerHealth"); Send$$anonymous$$essage("Die"); }
//animation.CrossFade(hitAnimation);
if (hitAnimation != "")
{
animation.CrossFadeQueued(hitAnimation, 0.1f, Queue$$anonymous$$ode.PlayNow); }
}
void Update(){ health = PlayersHealth.health; } void OnGUI() {
GUILayout.TextField(" HEALTH: "+ health,50);
} void OnDestroy () { } void $$anonymous$$illPlayer () { if (health < 1) GameObject.Find( "PlayerHealth"); Send$$anonymous$$essage("Die"); } } `
I'm assu$$anonymous$$g that the JS script is called PlayerHealth.js, and it has a static variable called health which you want to assign to the C# health - is this correct?
In this case, PlayerHealth.js must be placed in the folders Standard Assets or Plugins, and the C# script in another Assets subfolder - like Assets/Scripts, for instance.
But there's another solution that would avoid this arrangement: use Send$$anonymous$$essage. Take a look at my answer: I'm editing it to include this alternative.
Answer by pako · Oct 29, 2012 at 11:18 AM
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! I have a look at this when i get back from work :)
Answer by sobek · Oct 30, 2012 at 04:20 AM
Thank you both a lot, i went for Aldo Nalettos solution and it is working perfect! Im very greatful for this help :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613083017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Save static fields values 1 Answer
If there is only 1 instance of a class, should it be Static? - C# 2 Answers