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
0
Question by schwertfisch · Apr 03, 2011 at 08:11 PM · javascriptvariablesstaticvarcs0103

Accessing a js static var from a c# script

I have a static variable inside a JavaScript script. How can I access it from inside a C# script?

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

1 Reply

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

Answer by Ray-Pendergraph · Apr 03, 2011 at 08:16 PM

UnityScript files get compiled to classes underneath just like C# files. You can access a static field the same way as C# class. For unity script the class name is the file name so it should be <script file name without the js>.<field name>. Classes from unity script files never have a package. One thing to consider is the compile order because you can't breach the language boundary within a given compile phase. That's what SendMessage is for.

Edit: Here is an example of how this works. ACSharpScript.cs goes in some folder I define to ensure it's compiled last:

public class ACSharpScript : MonoBehaviour {
    void Update () {
        AUnityScript.COUNTER++;
    }
} 

...then, in Standard Assets I create a UnityScript file called AUnityScript.js :

public static var COUNTER:int = 0;
function Update () {
    print(COUNTER);
}

the C# is incrementing the static COUNTER field in the UnityScript class and an instance of the UnityScript (the instance created when you hang it on the graph) is reading the static field every frame. Hang these on the scene and try it... you will see COUNTER incrementing every frame.

Comment
Add comment · Show 6 · 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 schwertfisch · Apr 03, 2011 at 08:22 PM 0
Share

Thanks Ray but could you be more specific? I mean, I have a static var myStaticVar: int in a UnityScript (named $$anonymous$$yJSScript). Under certain conditions, I want a c# script to increase myStaticVar by one. Writing (in the C# script): $$anonymous$$yJSScript.myStaticVar +=1; doesn't work. What is the correct syntax?

avatar image Ray-Pendergraph · Apr 03, 2011 at 08:33 PM 1
Share

Doesn't compile or doesn't work? That looks right to me. You can access C# fields from unity script and unity script fields from C#. The important thing is the compile order described in the link. If one language is to call another in another language the target class must be compiled before the caller.

avatar image schwertfisch · Apr 03, 2011 at 08:41 PM 0
Share

At first, I had both scripts (UnityScript and C#) inside a folder named "Scripts" and I was getting the error: error CS0103: The name '$$anonymous$$yJSScript' does not exist in the current context. After that, I moved the UnityScript inside the Plugins folder (so that it's compiled first). This way I got rid of the error, but what I am telling the c# script to do ($$anonymous$$yJSScript.myStaticVar +=1) simply doesn't happen.

avatar image Ray-Pendergraph · Apr 03, 2011 at 09:04 PM 1
Share

$$anonymous$$ay be something else going on... we do this with a few UnityScript components that we did not write. Our code base is C# too. If you are making it past the compilation you should be O$$anonymous$$ with this. $$anonymous$$aybe try a different test case or something like setting the the thing to 20 and reading it from C#.

avatar image Ray-Pendergraph · Apr 04, 2011 at 12:07 PM 1
Share

@schwertfisch - I updated the post with a simple example.

Show more comments

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

No one has followed this question yet.

Related Questions

One of my scripts can't find the variable from this script 1 Answer

Help with variables 2 Answers

Cannot use Static var/ Unknown Idientifier 1 Answer

Is it possible to create new variables by incrementing? 1 Answer

Static Variables 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