- Home /
Accessing a C# field from javascript
I know there have been many questions like this, but none of them solved my problem. I have a game object with a C# script on it which I am trying to access a public field of from javascript. I am using a function in a javascript file (shown below) to access this script and get the field I want. However, Unity gives me an error saying that the script name that I am looking for is an unknown identifier.
function Move(person : GameObject, target : GameObject) {
person.GetComponent(typeof(AIFollow)).enabled = true;
person.GetComponent(typeof(AIFollow)).target = target;
}
Error: Unknown Identifier: 'AIFollow'
The person game object, when passed to the function, has the AIFollow script attached. I just want to enable that script and get modify a field of it. Any idea what I am doing wrong?
Thanks,
dylan92
You have to specify the order in which Unity compiles the scripts, if you want JS to access C# , or vice verca. See :
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
"All scripts that are compiled in this step have access to all scripts in the first group ("Standard Assets", "Pro Standard Assets" or "Plugins"). This allows you to let different scripting languages interoperate. For example, if you want to create a Javascript that uses a C# script: place the C# script in the "Standard Assets" folder and the Javascript outside of the "Standard Assets" folder. The Javascript can now reference the C# script directly. "
I tried doing this before and got an error saying, System.IO.Directory' does not contain a definition for
Delete'. I'm using the A* pathfinding algorithm on a web based project.
i don't have pro nor have tried the angry ant package so cannot help there. All I can suggest is make sure the class and the method you are calling are defined as public.
Answer by aldonaletto · May 15, 2012 at 03:59 PM
C# and JS can't see each other at compile time - but once compiled, the CIL code can be accessed by scripts written in any language. Unity compiles scripts in 3 waves, according to the folder the scripts are stored: Standard Assets and Plugins first, their Editor subfolders second, other folders in third place. You can place the C# scripts in Standard Assets, and the JS scripts in some custom Assets folder - like Assets/JsScripts, for instance. This will make C# scripts visible for JS, but not the other way around - only the scripts compiled last can "see" the others.
was just looking for a previous answer to link , and you answered this one too =]
http://answers.unity3d.com/questions/208383/referencing-a-c-component-script-from-js-script.html
I guess this does solve my problem, but now I have more problems with the A* pathfinding. Thanks to both of you.
@alucardj: yes, I answered that question a day ago (and a hundred times before...), but the UA search tool is so bad that usually I'm not able to find my own answers without googling for them! It's so boring that sometimes I decide to write a new one from scratch... UA could have some special tool to help us finding at least our own answers. The profile shows only the last 15 answers, and skipping pages to find a particular post takes so much time that you may be ninja'ed by other posters.
I have a package ( Google Play Games ) written in C#, but can't move the package folders into a 'first-compiled folder' because the references in the package code pop up errors?
Answer by spameater · Jan 30, 2014 at 12:45 PM
Is there a more elegant solution than shoving js and c# in different places just for the sake of execution order? Or isn't it possible to manually specify the folder names and their execution order?
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make text pop up for a few seconds? 3 Answers