JS vs C# Only one or the other?

I thought I read somewhere that you could not call C# from JavaScript, but I assumed that the two could otherwise peacefully coexist.

The following two screenshots seem to indicate otherwise. Earlier today, JavaScript would work but not C#. So I’m at a loss as to how Unity decides which language can be used.

One screenshot is from the Unity editor. It has a simple script that populates a text mesh with a string.

The other screenshot is from the same scene running on a Nokia Windows 8 phone.

Do I now need to make a decision on which language I want to use, and never mix the two, or is there something else going on?

-Chilton

1369692--68812--$Screen Shot 2013-09-26 at 10.59.02 AM.png
1369692--68813--$Screen Shot 2013-09-26 at 10.50.27 AM.png

Mixing is mostly tedious and you should try to avoid it. In the case that you want it, you need to consider that:

Thank you, Dantaus. I’m pretty familiar with the ins and outs of mixing code. Unfortunately, it appears that mixed code breaks one half or the other on Windows.

This same project works flawlessly on iOS, Android, Mac, PC, and Web builds. It only goes south on the Windows 8 Phone build.

-Chilton

@chilton, I got you wrong in that case. In that case it sounds like a bug that should be reported…

There are no issues at all mixing languages, as long as just you go one way. I use C# for “library” code (like static functions and so on) and Unityscript for “game/Unity” programming, since I’m far more productive using it than C# for that sort of thing.

–Eric

The error suggests that you have somewhere code like this:

public class Test
{
#if UNITY_EDITOR
public float var;
#endif
}

This creates a different serialization layouts, when you build WP8 from Editor, it will serialize var variable, but when you launch your app on WP8, the runtime doesn’t see the var variable, because assembly was compiled without UNITY_EDITOR, thus you read less data, than you should, on other platforms this was solved, but on Windows Store Apps/WP8, this issue is still, we might fix it for 4.5. But for now, you must be aware of such cases.

Hi Eric,

I’m glad to hear it!

I’m exactly the same way. I don’t know if I’ve done anything in just Unityscript or just C# in four years. I’m glad to know I’m at least not the only one doing that :slight_smile:

-Chilton

Hi Tomas,

Were it only so easy… :slight_smile:

There are only two scripts in this project. One in Unityscript/Javascript and one in C#.

I’ll post them, just a sec…

Enclosed is the scene and both of the scripts.
I hope this is something stupid I’m doing, but as of right now, I’ve tested everything I can think of. Please let me know what you find.

In the meantime I’m rewriting this app in C# so I can ship it already :slight_smile:

-Chilton

1369864–68840–$FunWithWindows8.unitypackage (4.83 KB)

I wouldn’t use Javascript at all for making Windows apps if I were you! There are a few bugs in compilation that are not fixed yet - especially involving strings. (4.2.1) You might get lucky and avoid those bugs but best to stick with C sharp.

I kind figured. I rewrote the entire app in C#.

But I now have the same problems >:(

I’m trying to figure it out now. Very, very frustrating.

Just a follow-up.

This was a show stopper and ultimately the lack of a solution was a deal breaker for my client.

This was the first app in 20+ years of software development I was unable to ship, due to the development environment failing to work properly. I received very little to no helpful feedback for the issues I ran into, and I see the problems I reported were not fixed prior to v4.3 shipping.

-Chilton

What is the case number of your bug, I’ll take a look.

Bummer, I think I’m seeing the same issue: everything runs great in the unity editor, and builds correctly in mono, and builds to other platforms, but give me errors when i build to windows … in this case i’m using CFInput (control pad item i bought from the store) and referencing it from my JS files, works great till i export to windows store, then all of a sudden it can’t find CFInput :sad:

Which version of Unity are you using. JS support has been added in 4.3. an you provide a more detailed error message?

Hey!

Thanks for the reply, my unity version is 4.3.4f1

error is /assets/scripts/robocontrollerscript.js(2,27): BCE0018: The name ‘TouchZone’ does not denote a valid type (‘not found’)

It works when building to other platforms, not sure why the win8store one fails. I’ve confirmed that the c# code is in the plugins, and i think putting this in assets/scripts should be ok?

Stick with C#. I don’t regret switching, my code is much cleaner now, and better designed due to the path c# sends you down. You’re forced to think a little more modular and this is beneficial long term.

Have you tried to write the same code in C#? It might be unimplemented API.

TouchZone is not Unity API, which means you have it defined somewhere in your scripts. If it’s defined in C# scripts in your code and you want to use it from JavaScript, you will have set compilation overrides to “Use .NET Core partially” in player settings for Windows Store Apps.

Ah thanks for the replies! I ended up rewriting in c# and it works, I didn’t realize the “use .NET Core partially” setting, thanks for the info, if i’m in the same situation again I’ll try it!