variable from "Java Script" to C Sharp Script (Sol

Discussion in 'Scripting' started by 08/15, Jul 13, 2010.

  1. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    Hi
    I want to access a variable in a Java Script from a C Sharp Script. I know a little bit about the Java Script in Unity, but I´m very stupid about C Sharp Script. So I know that I have to make a static var in Java Script, but I don´t know how to access the variable in C Sharp Script.
    Thanks for all replys.

    08/15
  2. Johker

    Johker

    New Member

    Joined:
    Jun 24, 2010
    Messages:
    24
    One would declare variables in JavaScript as such :

    Code (csharp):
    1.  
    2.    var myName : String;
    3.    private var myInteger : int;
    4.    var myBoolean : Boolean = true;
    5.  
    ...and in C#

    Code (csharp):
    1.  
    2.    public string myName;
    3.    private int myInteger;
    4.    public bool = false;
    5.  
    Note, in C# how you have to explicitly declare the access level. One can also instantiate variable with a value as with the myBoolean variable above.

    Hope it helps.
  3. Chris Sinclair

    Chris Sinclair

    Member

    Joined:
    Jun 14, 2010
    Messages:
    1,313
    In C#, you don't have to explicitly declare the access level, but they're private by default (opposed to JavaScript's public by default).
  4. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    Thanks for the replys,
    this forum is the best.

    Ok it helps especially how to declare Variables in C#. But my problem is not solved. I will try to make it better to understand.

    I have a Java Script like this :

    stativ var my_var = false

    in this script I do something with this variable.

    And I have this C# script in that I want to do something if the my_var Variable is true.
    So I have to access the my_var from the Java script.

    I hope you understand me?? My english is not the best.
    bye 08/15
  5. Chris Sinclair

    Chris Sinclair

    Member

    Joined:
    Jun 14, 2010
    Messages:
    1,313
  6. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    Sorry, but I didn´t find something helpful in this threads. The problems covered there are much more complicated than mine.

    However thanks for the reply.

    Nobody knows how to get a Variable from "Java" to C# ?
  7. returnString

    returnString

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    248
    In a given Javascript file, say, MyScript.js, you define a static var with static var myVar;.

    Then in the C# I believe you just use MyScript.myVar.
  8. Peter G

    Peter G

    Member

    Joined:
    Nov 21, 2009
    Messages:
    610
    I know the Unity documentation makes static vars sound like the way to go, and sometimes they are, but don't be mislead, most of the time you will want to use get component. Static vars are created as only one instance of a variable. So, let's say you have a bunch of enemies. If you attack one of them and they have a "static var health = 10". If you hurt the enemy for 6 points or something, every enemy will lose those 6 points. It acts in simple terms, like a voodoo doll. Whatever you do to one, you do to all of them.

    In most cases, you want to use GetComponent then access the variable that way, then you don't have to create a static var, only a public variable (in some cases you could use protected) so you can have each object affected individually.

    Now, to access a js script from a c# script, first you have to place your js script in a folder that gets compiled earlier such as standard assets and place the c# script outside of any folder. Make sure the js variable is public (it is by default), then use the following code to cache a link to it.
    Code (csharp):
    1.  
    2. public MyJSScript script;
    3.  
    4. void Start () {
    5.      script = GetComponent("MyJSScript"); //you have to use a string here.
    6. }
    7.  
    8. void Update () {
    9.      //to change any variable go:
    10.      script.var = someValue;
    11. }
    12.  
    PS. I think C# variables are protected by default. Please correct me though if I'm wrong.
  9. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    Hey big thanks for all your replys !!!!!

    Peter G your tip is great !!

    But in this case I need to have a static Variable.
    I have this java script
    Code (csharp):
    1.  
    2. static var test : float;
    3.  
    4. function Update () {
    5. }
    and this in c#
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class test : MonoBehaviour {
    6.     public javatest.test;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    17.  
    18.  
    and I get this message
    Assets/test.cs(5,29): error CS8025: Parsing error

    (This is a great forum! Thanks for having the time to help somebody how seems to be realy stupid like me :D )
  10. returnString

    returnString

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    248
    You want to read the variable somewhere. Remove your public javatest.test;

    Not very confident with C#, but...

    Code (csharp):
    1. void Update () {
    2.    print(javatest.test);
    3. }
    should check whether this is working.
  11. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    I get the message :

    The name `javatest' does not exist in the current context

    Any ideas ??

    In the Unity Script Reference it says :

    Global variables

    You can also create global variables using the static keyword.

    This creates a global variable called someGlobal.
    // The static variable in a script named 'TheScriptName.js'
    static var someGlobal = 5;

    // You can access it from inside the script like normal variables:
    print(someGlobal);
    someGlobal = 1;

    To access it from another script you need to use the name of the script followed by a dot and the global variable name.
    print(TheScriptName.someGlobal);
    TheScriptName.someGlobal = 10;

    But it won´t work :eek:
  12. Peter G

    Peter G

    Member

    Joined:
    Nov 21, 2009
    Messages:
    610
    Make sure you are compiling in the right order. Put the js script into the standard assets folder and take the C# script out.
  13. 08/15

    08/15

    New Member

    Joined:
    Jul 10, 2010
    Messages:
    23
    THANKS !!!!

    It works

    I had to put the java script in the sandard assets folder
    now everything works fine !!

    bye 08/51