Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
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 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 at 13:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.