- Home /
using public JS variables in C#
hey i dont know how to access public variables written in JS.
lets say i have a JS script that is this :
var varToAccess : float = 1.0;
function Start () {}
function Update () {}
i want to be able to access the varToAccess variable in C#. i thought this would work but it doesnt.
public float JSVar;
void Start () {
JSVar = GetComponent().varToAccess;
Debug.Log(JSVar);
}
void Update () {}
how do u access public vars written in another language?
Answer by iwaldrop · Feb 13, 2013 at 02:39 AM
Make sure your JS file is in a folder called Plugins or Standard Assets and then do the following:
using UnityEngine;
using System.Collections;
public class TestCS : MonoBehaviour
{
void Awake()
{
TestJS jsFile = GetComponent(typeof(TestJS)) as TestJS;
Debug.Log(jsFile.test);
}
}
where your JS file looks like so:
#pragma strict
var test = 5;
thanks for that :D, actually, how does the debug.log know what variable type test is?
C# can understand the var type as well. It's just that in C# land you can only use them in the scope of a method. The compiler works it all out at compile-time, so its no slower (at least functionally so) than declaring the type in your JS!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my score not being updated when the collision happens? 0 Answers
Acces C# via Unityscript 2 Answers
How to use Android Dual Joysticks? 1 Answer