# Unity 5: Accessing a C# variable from Javascript

I've set up a SQLite database with Unity. Into this I would like to add the current CPU Utilisation. The SQLite scripts are in Javascript and the method for finding the CPU utilisation is in a C# script, I have tried changing the javascript to C# but I am not that skilled. Ive tried several methods, each giving me errors:

private var csScript : ClientValues;

function Awake()
{
csScript = this.GetComponent("ClientValues");
}


using this method gives me the error "The name 'ClientValues' does not denote a valid type ('not found')"

var curValue : int;
var gObject2 : GameObject;

function Update()
{
var cSharpScript = gObject2.GetComponent("Clientvalues");
curValue = cSharpScript.myValue;
}


I get the error: 'myValue' is not a member of 'UnityEngine.Component'

Is this something to do with me using Unity 5? What can I do to have the Javascript access the C# value?

You have to cast it to ClientValues type.

var cSharpScript = (ClientValues)gObject2.GetComponent("Clientvalues");


or

var cSharpScript = gObject2.GetComponent("Clientvalues") as ClientValues;


First one causes runtime error if cast fails and the second one returns null if it fails. Important difference. But both should do the trick.

• Thanks for the reply! Whenever I try the first i get a ';' expected, Insert a semicolon at the end. Whenver I try the second I get a The name 'ClientValues' does not denote a valid type ('not found'). Where am I going wrong here? Does it matter where I place these statements, I have both at the top of the script.
– CH99
May 13 '15 at 13:09
• Apologies got it working now! For anyone else reading my problem was that my C# script with the required value wasnt in my plugins folder. Placing it in there sorted this. Ty Katu :)
– CH99
May 13 '15 at 13:24