Search Unity

[SOLVED] How do I Get C# Variable from JS Script?

Discussion in 'Scripting' started by ChrisIsAwesome, Jul 25, 2017.

  1. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    I'm using 5.6.1f1

    So I have an object that has a C# script and a JS script on it (because some things are easier to code in C# while others can be easier in JS). However, I have run into an issue where you can't reference a 2nd script on an object.

    Script 1 name is "ballControlCS"
    Script 2 name is "ballControlJS"

    The error is with this code:
    Code (JavaScript):
    1. hasBeenPressed = ball.GetComponent (ballControlCS).hasBeenPressed;
    I get a console error saying:

    BCE0005: Unknown identifier: 'ballControlCS'.

    I don't understand. I can reference the ballControlJS script just fine, but referencing ballControlCS is impossible. Is this a known bug or am I just oblivious to the obvious?

    PS: Might be a C# issue since I'm a C# newbie and have had issues before with referencing a C# script from JS.

    -- Chris
     
  2. ChrisIsAwesome

    ChrisIsAwesome

    Joined:
    Mar 18, 2017
    Posts:
    184
    EDIT: I have found that components from different languages cannot be seen by another language (ie. C# variable can't be recognized by a JS script, vice versa).

    So the question now is how do I get a C# variable from a JS script?

    Any help would be greatly appreciated!
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can reference components of a different language, but the two languages are not compiled at the same time so can't talk in both directions. You can though have 1 way communication by manipulating the compile order of your scripts.

    https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    It would be highly recommended though to just standardize around a single language (cough cough.... C#) to avoid these kinds of problems.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Did somebody order coffin nails?

    Yes, go with C# for everything. The more you learn, the more you'll like it. It's not worth the hassle to try maintaining two languages in your projects (trust the forum's experience on this!), and UnityScript isn't going to get any love from UT.

    If you have trouble converting scripts/can't find a replacement, just ask again in this sub-forum :)
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Agree 100% with the "go with C#" sentiment... if you think something is easier in UnityScript, it is likely just because you haven't been exposed to enough C# to see that it is in fact likely far easier in C#, and more reliable too.
     
    orb likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There are several basic ways to communicate between scripts of different languages.
    • Have one of the scripts compile earlier by putting it in plugins, or by compiling it into a dll. Scripts compiled later will be able to access it.
    • Use SendMessage to invoke methods on scripts with different languages. SendMessage is string and engine based, so it is language agnostic.
    • Store data in a place both scripts have access too, such as playerprefs or a text file.
    • Reflection operates after the scripts are compiled. So it should not care what language the scripts were in originally. I haven't actually tried this, but it should work.
    You'll note all of these methods are messy and have significant hoops to jump through. The best option is to convert everything to C#. But if for some reason you can't, there are hoops you can jump through in the short term*.

    *Long term Unity has given clear indications that UnityScript will be discontinued.