• 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 071813g · May 14, 2012 at 12:51 PM · global variable

Accessing global variable from java in c#

I believe there is similar question in the forum, but I just can't find it, sorry.

My question is how do I access a global variable from a java script in a c# script.

For instance, I have a global variable called "world" defined in a java script called "A". Then how do i access this variable in a c# script. I guess it need to be declared in c# again?

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · May 14, 2012 at 01:33 PM

Javascript and C# can't see each other at compile time, but scripts already compiled become pure CIL code and are visible to other scripts independently of language.
Unity compiles scripts in 3 consecutive waves, according to the folder they are: Standard Assets and Plugins first, Editor subfolders next, then other folders. You can place the JS script in Standard Assets or Plugins, and the C# one in some other Assets subfolder.
If you only need to access global variables (static?), place all of them in a script and save it in Standard Assets, and place all other scripts in a custom folder in Assets (Assets/Scripts, for instance).

Comment
Add comment · Show 1 · 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 071813g · May 14, 2012 at 03:56 PM 0
Share

wow I didn't know that, thanks for your helpful information!

avatar image
1

Answer by bompi88 · May 14, 2012 at 01:36 PM

If both scripts are one the same GameObject, you can do something like this. And you should put your A.js in a folder called "Plugins", because C# is built before JS. You can read more about Script compilation here. This is non-static method of doing it. Edited to needs: two seperate GameObjects.

 // A.js
 public var hello : String = "This is a string";

 //B.cs
 using UnityEngine;
 using System.Collections;
 
 public class B : MonoBehaviour {
     public GameObject go;
     private A Ascript;

     void Start () {
         Ascript = go.GetComponent<A>();
         Debug.Log(Ascript.hello);
     }
     
 }


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 071813g · May 14, 2012 at 03:57 PM 0
Share

They are not on the same GameObject, but I will try it out myself when I'm home, thanks for your help!

avatar image bompi88 · May 14, 2012 at 04:11 PM 1
Share

Edited my answer above. $$anonymous$$aybe it helps.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

questions about referencing global variables 1 Answer

Issues sharing variable between 2 scripts 2 Answers

Using Find() or dragging object into slot via Inspector using public variables? 1 Answer

Is there a better way to access global variables? 1 Answer

Are C# Global Variables Safe In Unity? 5 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