0
\$\begingroup\$

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:

(http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access)

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')"

and if I use (http://answers.unity3d.com/questions/272122/can-i-pass-a-value-from-a-c-script-into-a-java-scr.html)

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – CH99
    May 13, 2015 at 13:09
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – CH99
    May 13, 2015 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .