- Home /
Accessing java script variables in a c# script
I have a java script that I need to be able to access info in a c# script. I'm not even sure if this is possible, but if it is, I'm haven't the slightest on the syntax.
Ex. StateControl.js
enum GameStates { Idle = 0, Presentation, Spin, Results, SpinWithCamera, Offline };
MainControl.cs
public class Control : MonoBehaviour
{
//this is where the accessing problems are, I would like to use the enum from
//StateControl.js
void checkStates(GameStates newState, GameStates oldState)
{
//do stuff
}
}
Answer by testure · Sep 09, 2011 at 06:31 PM
I'm not 100% sure about JS syntax either, but so long as the enum is public (i think in JS, if it's outside the class it's automatically public?), a C# script should still be able to access it. The reason you can't access it now is probably because it thinks it's a member of StateControl.. so you could still technically access it using StateControl.GameStates
hope that helps
That's what I would think too, but my c# script can't seem to find any of the js "classes" (not sure if that's what the term is, very unfamiliar with js). $$anonymous$$y new c# script is in the correct folder, so it's not that. Thanks for trying to help.
Hrm, this seems to be related to what you're asking, it might help:
http://answers.unity3d.com/questions/10856/cant-find-namespace-accessing-javascript-from-c-or.html
Answer by kieblera5 · Sep 09, 2011 at 07:08 PM
Put the JS in the Standard Assets folder. That's what I've had to do to access JS from C#.
yeah, that correlates with what I was reading from Duck in that other answer. Standard Assets is an earlier compilation folder- so it would make sure the class exists when you try to use it.
Personally I always just port JS stuff over to C# when I need something that's already written in JS- but that's just me :D