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 princeadj14 · Nov 30, 2013 at 03:01 PM · variablecallingassign-variablejava to c#

How to call variable from java script to c# Script? :(

//Java Script Variable

     public var papers : int = 0; 

//C# Script that needs the variable

     GUILayout.BeginHorizontal();
     GUILayout.Label (pl.PlayerName);

     papers = pl.Score;
     GUILayout.Label (papers.ToString());
         
     GUILayout.EndHorizontal();
Comment
Add comment · Show 1
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 princeadj14 · Nov 30, 2013 at 01:59 PM 0
Share

Thankyou In Advance please help me...

2 Replies

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

Answer by chillersanim · Nov 30, 2013 at 03:09 PM

Access the gameobject which contains the javascript object you want to use. Then get the script with GetComponent(type) and cast it to the correct type. Then just get the variable from the object.
Perhaps you should also check if the object is not null.

If you make your variable static, then you can directly access the variable with the class name (Class.papers).

I haven't tested if you can access the javascript from c# but I don't see a reason why that wouldn't be possible...

Greetings Chillersanim

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 princeadj14 · Nov 30, 2013 at 03:39 PM 0
Share

sir can you $$anonymous$$ch me how to find an object to get the component that i want to use and get the variable in that particular script in c# I know how to that in Java script but i found it difficult in C#

avatar image chillersanim · Dec 06, 2013 at 12:21 PM 0
Share

Edit:
$$anonymous$$oved the code example outside, so that the text + code is better displayed.

avatar image
1
Wiki

Answer by chillersanim · Nov 30, 2013 at 05:21 PM

There are multiple possibilities to access a field of another script, here are a few:

  1. Make a public field in the script that needs the other script. This field should be of type "gameObject". Make a reference in the unity scene to the searched object (drag the searched object into the inspector field of the first script). Then access the field from the script. Get the script from the object and you can access the searched field.

  2. Use the singleton pattern for the searched script

  3. Make a static class and access the field directly over the class

If you have multiple instances of the searched script (with the papers-field), then you should use the first possibility.
Else if you have only one instance of the searched script (and you know that this will not change) then I would reccomend you the second possibility.
The thirt possibility is similarly to the second case, but is not a clean possibility and should be avoided.


Code Examples


1)
This code is for the class that needs to get the value.

 public GameObject targetObject;
 public int GetPapers () 
 {
     if (targetObject != null)
     {                
         var script = targetObject.GetComponent<YourScript>() as YourScript;
         if (script != null)
         {
             return script.papers;
         }
     }
     return -1;
 }

The "targetObject" is a field of the accessing class. The method "GetPapers" gets you the value of the variable, it returns -1 if the targetObject is null or the script was not found.


2)
This code is for the class that contains the value.

 // Singleton example
 public class YourScript : MonoBehaviour 
 {
     // the local (and only) instance
     private YourScript instance;

     // declare the fields as normal and access the with the instance
     public int papers;

     // Access the object with this readonly property
     public static YourScript Instance
     {
         get
         {
             // At the first call, create a new instance
             if (instance == null)
             {
                 instance = new YourScript();
             }
             return instance;
         }
     }

     // Make the constructor private so there cannot be a second instance
     private YourScript()
     {
         // Do your initialisation here
     }
 }

With such a class, you can easely access all fields and methods.
Just get the value with:

 var a = YourScript.Instance.papers;

The advantage of this patern is, that you can have a constructor and initialize your fields and variables (a static class cannot have a constructor).


3)
This code is for the class that contains the value.

 public static class YourScript
 {
     public static int papers;
 }

Then you can get the value with:

 var a = YourScript.papers;



Note that you cannot add the script to a gameobject if you use possibility 2 and 3. They are only for the case that you need not multiple instances. If you want to add the script to a gameObject, you should use possibility 1.

If you want to contribute or correct something to this comment, then feel free to edit it.

Greetings
Chillersanim

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to assign a private transform of my player in script? 1 Answer

Callling a variable and setting values only once, not every frame. 3 Answers

Says that variable has not been assigned(but I already did). 1 Answer

Variable doesn't change in Unity methods 0 Answers

Calling a variable from another script at an instance. 0 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