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());

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):


#pragma strict

@script RequireComponent( AudioSource )
public var papers : int = 10; //Varible

function Start () {


function Update () {



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());	

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