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 07:55 AM · java to c#js-c#c# to javascript

Can you Help me about how to get the variable of Java Script to C# script?

 //Java Script 
 #pragma strict
 @script RequireComponent( AudioSource )
 
 public var papers : int = 0; //Varible
 //-----------------------------------------------------------
 //C# Script
 var Score;
 
 Score= papers ;
 
 GUILayout.Label (pl.Score.ToString());
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 princeadj14 · Nov 30, 2013 at 07:55 AM 0
Share

I Need this for my Score my problem is my Scoreboard is made in C# Scripting and my $$anonymous$$y score variable is in Java script to show in my score board.

please help me Thank you very much! in advance... what is the problem? :((

avatar image fafase · Nov 30, 2013 at 07:59 AM 0
Share

Your var declaration in C# won't do. Then you need to look into http://answers.unity3d.com/questions/184738/javascript-call-a-method-from-c-sharp-and-vise-ver.html

avatar image princeadj14 · Nov 30, 2013 at 08:42 AM 0
Share

what if sir getting from javascript

Javascript public var papers

C# GUILayout.Label (pl.papers.ToString());

2 Replies

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

Answer by nastasache · Nov 30, 2013 at 06:34 PM

Hi princeadj14,

You probably got the 'The type or namespace name could not be found' error.

It that, it's about the visibility of javascript component due the compilation order.

A solution: move your javascript script under "Standard Assets" folder. If not have the folder, create it.

Then use GetComponent(). As below (suposing you have both scripts attached to the same object):

JsScript.js:

 #pragma strict
 
 @script RequireComponent( AudioSource )
  
 public var papers : int = 10; //Varible
 
 function Start () {
 
 }
 
 function Update () {
 
 }

CsScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class CsScript : MonoBehaviour {
 
     
     public int score;
     public JsScript jsComponent;
     
     // Use this for initialization
     void Start () {
         jsComponent = gameObject.GetComponent("JsScript") as JsScript;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnGUI() {
         score = jsComponent.papers;
         GUILayout.Label (score.ToString());    
     }    
 }
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 fafase · Nov 30, 2013 at 06:47 PM

Unity allows you to use different languages for your application but this is at a certain price.

Unity converts all scripts to IL which is assembly language for .NET. At that level all languages are using the same syntax. So they can interact together. But the problem is that before being convert they cannot see each other. So when writing C#, it cannot see what is already in Js, it will once both are down to IL. But until then they are different.

Unity has an order of compilation, what is contained in Standard Asset(SA) r Plugins is compiled first. Then whatever else is compiled next. But then the issue is that what is in the SA will not be able to see other script since it will be compiled first.

For instance ScriptA.cs is in Sa and ScriptB.js is in its own folder, if ScriptB has a reference to ScriptA, then at compilation ScriptA is already compiled since it was in SA and ScriptB can read it in IL language. BUT, if ScriptA has a reference to ScriptB, it is not able to fulfill that requirement because ScriptB is still in Js form and not yet in IL.

So the conclusion of this, if you need two-ways communication with script, they have to be of same language, if only one looks down to the other then this one is in SA while the other is somewhere else.

Accessing the other guy is done as usual:

 ScriptA.cs in SA
 
 public class ScriptA:MonoBehaviour{
    ScriptB scB;
    float varInB;
    void Start(){
       scB = GameObject.Find("ObjB").GetComponent<ScriptB>();
       varInB = scB.memberInB;
    }
 }
 
 
 ScriptB.js in any other folder
 
 var memberInB:float; // This is public to be accessed
 // var scA :ScriptA; <- Not possible
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

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

Related Questions

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

Astarpathfinding Scan all graphs 1 Answer

How to call/Execute from C# to JS? 1 Answer

Gameobject variable on C# 1 Answer

Error with converted Function? 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