- Home /
Accessing JavaScript variable from C# and vice versa.
Forexample:
Test01JS _a = GameObject.Find ("Test01").GetComponent<Test01JS>();
print (_a.test01JS);
It says: The type or namespace name "Test01JS" could not be found. etc.
Answer by jonas-echterhoff · Feb 22, 2011 at 10:40 AM
C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible. However, you can affect the order of compilation by moving scripts into special folders which are compiled earlier. You could move your Test01JS script to a folder called "Plugins" then it works.
Small note - c# and js are unable to see each other in the same pass. If you try add a statically typed reference to a c# type in a js script, it'll fail too.
for some reason, this method doesn't work in Unity 4.6. Is there another way?
Answer by fil · Oct 17, 2012 at 12:02 PM
Hi,
see this tutorial: http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access
download the example and you'll see that is very simple
Yah it is very simple... Thank you so much for this link... You save my day...
Answer by Snownebula · May 18, 2015 at 08:00 AM
Assets/_Scripts/JumpPoint001.cs(6,16): error CS0246: The type or namespace name `levelLoader' could not be found. Are you missing a using directive or an assembly reference?
levelLoader is my js file in the Standard Assets folder. Am I doing it wrong? using UnityEngine; using System.Collections;
public class JumpPoint001 : MonoBehaviour {
port function.
public levelLoader loadCheck;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other){
loadCheck = this.GetComponent<levelLoader>();
}
}
Did you end up fixing this? I have the same problem.. Tried puting the javascript script into the plugins folder and the standard assets folder and haven't been able to properly find it in my code.
It should work as long as;
Your UnityScript file is either in plugins or Standard Assets
and your C# file is not inside those folders.
If that's the case the C# file should be able to access the UnityScript class. Of course it's always just one way. So either the C# script can see the UnityScript (in which case the UnityScript can't see the C# script) or the other way round. You can't go forth and back between different languages.
It's best to avoid this case in general and port either script over to the other language. If you have problems "converting" UnityScript to C#, that's easy.
Answer by waqaswaqas · Feb 19, 2016 at 01:47 PM
Save GameObject States in a file you want to get in cs or js Just import this file Js or C# you want