Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dylan92 · May 15, 2012 at 03:34 PM · c#javascript

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · May 15, 2012 at 03:54 PM 0
Share

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. "

avatar image dylan92 · May 15, 2012 at 04:19 PM 0
Share

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.

avatar image AlucardJay · May 15, 2012 at 04:23 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · May 15, 2012 at 04:05 PM 0
Share

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

avatar image dylan92 · May 15, 2012 at 04:35 PM 0
Share

I guess this does solve my problem, but now I have more problems with the A* pathfinding. Thanks to both of you.

avatar image aldonaletto · May 15, 2012 at 11:40 PM 0
Share

@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.

avatar image Andrea_Marchetti · May 23, 2017 at 02:14 PM 0
Share

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?

avatar image
1

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Trouble converting player controller from JS to C# 1 Answer

Accessing scripts in the same game object 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges