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”
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.
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?
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.
It would be highly recommended though to just standardize around a single language (cough cough… C#) to avoid these kinds of problems.
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
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.
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.