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
5
Question by BitMiller · Feb 22, 2011 at 10:30 AM · c#-and-js

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.

Comment
Add comment
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

4 Replies

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

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.

Comment
Add comment · Show 2 · 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 Mike 3 · Feb 22, 2011 at 11:03 AM 1
Share

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.

avatar image chautran94 · Aug 17, 2015 at 04:28 AM 0
Share

for some reason, this method doesn't work in Unity 4.6. Is there another way?

avatar image
5

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

Comment
Add comment · Show 3 · 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 vfxjex · Jan 22, 2015 at 02:29 PM 0
Share

Yah it is very simple... Thank you so much for this link... You save my day...

avatar image sujit11dec · Mar 18, 2015 at 01:06 PM 0
Share

Thanks Fil....Really nice tutorial link.

avatar image GAGO_OGAG · Aug 10, 2016 at 11:16 AM 0
Share

Thx alot!!

avatar image
0

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>();
     }
 }
Comment
Add comment · Show 2 · 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 andrew_2992 · Dec 23, 2016 at 03:21 AM 0
Share

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.

avatar image Bunny83 andrew_2992 · Dec 23, 2016 at 05:38 AM 0
Share

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.

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

7 People are following this question.

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

Related Questions

Make C# Class Globally Available on JavaScript 0 Answers

iOS / dynamic typing issue: not a member of 'UnityEngine.Component' 2 Answers

is not a member of 'UnityEngine.Component' 1 Answer

getting a variable from a c# script with a Js 2 Answers

passing variable from javascript to C# 1 Answer


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