Unity Community


Results 1 to 13 of 13

  1. Posts
    23

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

    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. Posts
    24
    One would declare variables in JavaScript as such :

    Code:  
    1.   var myName : String;
    2.    private var myInteger : int;
    3.    var myBoolean : Boolean = true;

    ...and in C#

    Code:  
    1.   public string myName;
    2.    private int myInteger;
    3.    public bool = false;

    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. Location
    Toronto, Ontario, Canada
    Posts
    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. Posts
    23

    Hi

    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. Location
    Toronto, Ontario, Canada
    Posts
    1,313


  6. Posts
    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. Location
    Frankfurt am Main
    Posts
    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. Location
    Cincinnati USA
    Posts
    609
    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:  
    1. public MyJSScript script;
    2.  
    3. void Start () {
    4.      script = GetComponent("MyJSScript"); //you have to use a string here.
    5. }
    6.  
    7. void Update () {
    8.      //to change any variable go:
    9.      script.var = someValue;
    10. }

    PS. I think C# variables are protected by default. Please correct me though if I'm wrong.
    Unity Answers a great companion site to the forum.

    Sign up today.

    http://answers.unity3d.com/


  9. Posts
    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:  
    1. static var test : float;
    2.  
    3. function Update () {
    4. }

    and this in c#
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test : MonoBehaviour {
    5.     public javatest.test;
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15. }

    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 )


  10. Location
    Frankfurt am Main
    Posts
    248
    You want to read the variable somewhere. Remove your public javatest.test;

    Not very confident with C#, but...

    Code:  
    1. void Update () {
    2.    print(javatest.test);
    3. }

    should check whether this is working.


  11. Posts
    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


  12. Location
    Cincinnati USA
    Posts
    609
    Make sure you are compiling in the right order. Put the js script into the standard assets folder and take the C# script out.
    Unity Answers a great companion site to the forum.

    Sign up today.

    http://answers.unity3d.com/


  13. Posts
    23
    THANKS !!!!

    It works

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

    bye 08/51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •