- Home /
Getting variables from c# to js
So folks,
I know there are a ton of those answers on this and I tried so far to follow these, but for some reason I the c# variable can not be called properly from the JavaScript.
I have moved the c# joystick script (thanks to GibTreaty) into the Plugins folder at the root of the whole assets folder.
The javaScript that is handling the variable from the Joystick.cs to move the character is placed under Assets/Resources/Scripts. It would be a pain to rewrite all this to c# despite that I always code in js. Should change that. '_'
With this from what I have read here the scripts are executed in the correct compilation phase.
I changed the Vector2 to public, so the js can have access to that.
Here the associated code from the JS:
function Start(){
axis = GameObject.Find("JoystickMove").GetComponent.<Joystick>()._axis;
}
function Update(){
Debug.Log(axis.x);
}
It always prints 0 for the x-value of the Vector2, even when I move the joystick around.
Any suggestions?
Answer by RLin · Aug 10, 2015 at 02:06 AM
Axis is only set once in start. You need to set it in update for the value to be updated correctly.
Thank you for pointing it out!!! I kept in $$anonymous$$d that the initialsation was actually an object reference (had it in the past as I worked with my own Joystick script in Js by using scrollRects), so I do not have needed to call it every frame. :D
Answer by seth_slax · Aug 10, 2015 at 04:04 AM
The easiest way I've found to get C# and JS to talk to each other is simply with SendMessage. What you've written hasn't worked for me in the past either. SendMessage can be somewhat CPU taxing if you're doing it a lot, but it's never given me any trouble.
If that doesn't work, you could try the reverse of what you have; get the C# script to set the value within your JS script.