Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by sobek · Oct 28, 2012 at 07:51 PM · c#staticjava

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? :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pako · Oct 28, 2012 at 08:38 PM 0
Share

You say that you have tried to apply what is in the documentation. What is your code like for doing this?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);




Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sobek · Oct 29, 2012 at 12:18 AM 0
Share

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"); } } `

avatar image aldonaletto · Oct 29, 2012 at 12:23 AM 0
Share

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?

avatar image sobek · Oct 29, 2012 at 01:44 AM 0
Share

Yes that is correct :)

avatar image aldonaletto · Oct 29, 2012 at 02:43 AM 0
Share

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.

avatar image sobek · Oct 29, 2012 at 03:33 PM 0
Share

Great!! I have a look at this when i get back from work :)

avatar image
1

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());
 
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sobek · Oct 29, 2012 at 03:33 PM 0
Share

Thank you! I have a look at this when i get back from work :)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

is static variables take time to be changed?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges