problem with type casting between scripts

Hi there.

For very simple multi-language support in my project, I’ve added a function to the GameController script (JS) that returns a String depending on the language. But every time I try to assign that returned String to a GUIText (or even to a String variable I get a type cast error.). I’m new to JavaScript.

Here’s the sample code for GameController.js

function Hello()
{
	return "hello";
}

Code from the actual script attached to another GameObject

var x : String = GameObject.Find("GameController").GetComponent("GameController").Hello();

Even that gives me an “InvalidCastException: Cannot cast from source to destination type.”

Thanks in advance,

qgi

Try this

var hello = Hello();

function Hello() 
{ 
   return "hello"; 
}
var x : String = GameObject.Find("GameController").GetComponent ("GameController").hello;

I’m not sure why yours doesn’t work.

Daniel,

Thanks for the help! That really does the trick for that bit of code.

But what do I do if I need to pass a parameter?
What I really want to do is something like calling Hello(language:int) that returns a String in a language (I use a “dictionary” Array that returns a String based on the given language).

I also tried to declare my function as

function Hello() : String
{
     return "Hello";
}

but this didn’t do it either (same exception, no complains about the “: String” bit though)

Thanks again!
qgi

I’m working with the same things here and would love some further insight.

I just tried:

var x = GameObject.Find("GameController").GetComponent("Test").Hello(1);
print (x);

with “Test” being:

function Hello (which : int) {
	if (which == 1) {return "Hello";}
	else {return "Hola";}
}

and it works fine. That method uses dynamic typing, even if you specify “var x : String”, so make sure you’re not using “#pragma strict”;

–Eric