x


Referencing a c# component script from JS script

How do you reference a c# component script from a JS script? Do you need to store the reference as you would with a JS component, to prevent having to fetch GetComponent each time?

--

Trying the usual below for a c# script named CSharpComp.cs, I get this error: 'CSharpComp' does not denote a valid type ('not found').

myfile.js contains:

var csharpcomponent:CSharpComp;

function Start(){

csharpcomponent = GameObject.Find("MainObj").GetComponent(CSharpComp);

}

more ▼

asked Jan 21 '12 at 04:42 PM

ina gravatar image

ina
3.3k 492 551 602

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

C# and JS can't see each other at compile time (different compilers are used for each language), but already compiled scripts can be seen by any language. If you place the C# script in Plugins or StandardAssets, and the JS script in another Assets folder (Assets/Scripts, for instance), the C# script will be compiled in the "first wave", thus the JS script will know it and compile ok (see Script Compilation). NOTE: this is a one way via: the CS script won't be able to see the scripts not yet compiled (JS or C#).
If you just need to call some CSharpComp function, SendMessage("FunctionName", argument) can be used. This is a very limited alternative, since SendMessage accepts only a single parameter, but it works independently of language or compilation order (it's a runtime feature).

more ▼

answered Jan 21 '12 at 04:56 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Also, I read SendMessage should be used sparingly since it is very slow and impacts performance

Jan 21 '12 at 04:59 PM ina

It looks like this does not work exactly, as MainObj contains the CSharpComp.cs that myfile.js is trying to refernece... the GetComponent does not work!

Jan 21 '12 at 05:14 PM ina

It's weird! I tested a JS script in Assets/Scripts and it found MouseLook.cs without any problem (MouseLook.cs is stored in Standard Assets/Character Controllers/Sources/Scripts).
And you're right about SendMessage: it must check all scripts and look for the specific function in each one, thus using SendMessage frequently isn't a good idea.

Jan 22 '12 at 02:27 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

You can set the assembly dependencies in the MonoDevelop project. Right click on the 'References' folder under the Assembly-UnityScript project and 'Edit Preferences'.

Again, cyclic references are not allowed so you can't make both projects see each other.

Anyway, this is bad practice to have a project with both JS and C#. You should consider translating all scripts to one unique language.

more ▼

answered Jan 21 '12 at 07:32 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4174
x406
x263
x256
x132

asked: Jan 21 '12 at 04:42 PM

Seen: 1954 times

Last Updated: Jan 22 '12 at 02:27 AM