x


"Can't find namespace" - accessing Javascript from C# (or C# from Javascript)

I'm trying to call a function or variable from a JavaScript in a C# class.

I've moved the JavaScript file into the Standard Assests folder but I still get a "can't find namespace" error.

I have made sure that the files are correctly named and that they are called correctly in the script. Is there any other way to fix this?

more ▼

asked Jan 30 '10 at 01:23 PM

Keota gravatar image

Keota
76 1 3 4

(Edit: made title more generic, and added "or variable" because the answer is equally applicable to functions and variables)

Jan 30 '10 at 05:49 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

First, I'm assuming you've read this manual page:

Overview : Script Compilation (Advanced)

It describes the script compilation order. I'm linking it above for others which may encounter this answer from search terms which match the question.

In summary:

Whichever script has the function or variable that you want to access, that is the script which must go in the earlier compilation pass. So, if it's the c# script which contains the function you want to call, and the Javascript class is trying to call it, you'd move the C# file into an earlier-pass folder (such as "Plugins").

If the Javascript class contains the function or variable, and you want to call it from a C# script, it's the Javascript script which must go in an earlier-pass folder.

Note that this is a one-or-the-other situation. You can't have references both ways between two scripts of a different language.

Make sure your scripts aren't both in different folders which fall into the same compilation pass. Eg, both "Plugins" and "Standard Assets" are compiled in the same pass.

If you have your scripts arranged in the correct compilation order, and you're not trying to make "both-way" references, then the only other option I can think of is that you might have a typo either in your script filename, or in your references to that script type in your other script.

more ▼

answered Jan 30 '10 at 05:38 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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

As a corollary to Duck's answer:

If you find you need C# for certain programmatic problems, the 'both-way' reference problem is a great reason to switch entirely to C#.

We made it for about a year with Unity before this really bit us, but eventually we took a day and ported all the old javascript code to C#. That was about 2 years ago - and we've never looked back. In the end it made working in Unity a lot simpler, particularly to avoid this sort of issue.

more ▼

answered Jan 30 '10 at 10:14 PM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

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

I'm trying to do this and i have placed the desired JS in the Standard Assets folder. The script that is accessing it is in the My Scripts folder which is not inside any folders. Somehow upon build i still get the error that says.

"The type or namespace name 'PlatformerController' could not be found (are you mising a using directive or an assembly reference?)"

public PlatformerController player;
more ▼

answered Mar 07 '11 at 03:18 PM

user-10025 (google) gravatar image

user-10025 (google)
1

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

Thank you for wonderful advice.

And sorry for being stoopid... But where is the plugin directory located? Under Library? I have a folder that is called Unity Projects, and in there all the data that I make go. There is no Plugin directory, and I can find one under Program Files either.

more ▼

answered Jul 20 '10 at 08:51 AM

Jens T gravatar image

Jens T
138 6 7 17

Found out that the Plugin directory also compiled earlier. You may create a folder in your Assets folder, and put your C# files there.

Jul 20 '10 at 09:09 AM Jens T
(comments are locked)
10|3000 characters needed characters left

Hello, I found this tutorial and it quite simple: http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access

download the example to see it working

more ▼

answered Oct 17 '12 at 12:02 PM

fil gravatar image

fil
31 2 2 3

(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:

x5061
x108
x71

asked: Jan 30 '10 at 01:23 PM

Seen: 19216 times

Last Updated: Oct 17 '12 at 12:02 PM