Can C# and JS work together?

Can C# and JS work together?

Yes, but if you want to get the script that uses a different language, you need to call GetComponent("OtherScript") instead of GetComponent(OtherScript).

That is only necessary if the other script is not compiled before the script that's calling it See Ostagar's answer below. If a C# script is in Plugins, you can refer to it via GetComponent(Classname) from a UnityScript file that's outside the Plugins folder. But if the .js script is also in Plugins, or you want your script that's in Plugins to refer to a script outside Plugins, then you have to use GetComponent("Classname") and communicate via SendMessage (you can't call the methods directly).

related: http://answers.unity3d.com/questions/21676/is-it-possible-for-c-script-using-class-written-in-js

3 Answers

3

"I mean as in conjunction with each other, like using a C# function from a JS script"

It's a little messy, but if you put them in the appropriate folders and have them call each other using GameObject.SendMessage(), you can. For more details read:

http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

I would try to avoid this situation whenever possible.

Two scripts one JS one C#, on a single GameObject, will work together. Is that what you wanted to know?

Yes, they can work together. But I'd avoid doing it as it can get really messy. I just convert everything to C#.

Let's consider we have everything in JS and need to add complex pre-made C# script. We will probably be safe just adding it to `Standard Assets` to get it first on the compilation order. The mess would begin if we wanted to make that C# script to call anything else on our project.

Be sure to declare as public any class you want to call from another one, or using SendMessage instead - which is arguably slower. Those are same rules we apply while using same language, tho.

Unity seems to be advancing to ease up those compilation order issues, but I'm afraid they'll never completely vanish.