- Home /
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());
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? :((
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
what if sir getting from javascript
Javascript public var papers
C# GUILayout.Label (pl.papers.ToString());
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());
}
}
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613123109im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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