Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by Keota · Jan 30, 2010 at 01:23 PM · compilingjavascript-specific

"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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Jan 30, 2010 at 05:49 PM 0
Share

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

avatar image user-10025 (google) · Mar 07, 2011 at 03:18 PM 0
Share

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 $$anonymous$$y 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;

3 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

Answer by duck · Jan 30, 2010 at 05:38 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image privman · Aug 07, 2013 at 02:03 PM 3
Share

The documentation page has moved, it is now at: http://docs.unity3d.com/Documentation/$$anonymous$$anual/ScriptCompileOrderFolders.html

avatar image chautran94 · Aug 16, 2015 at 03:38 PM 0
Share

I guess this answer doesn't apply in unity 4.6 cuz it doesn't work for me. Can you update the solution?

avatar image Andrea_Marchetti · May 22, 2017 at 02:36 PM 0
Share

What if I have a package (Google Play Services) which has references to some file that must be into a certain folder in the Assets folder? If I try to reference it from my javascript I get namespace not found. If I try to set the package's folder into the Plugins folder then I get the error that a code is trying to access an inexistent folder.

avatar image
9

Answer by Brian-Kehrer · Jan 30, 2010 at 10:14 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jens T · Jul 20, 2010 at 08:51 AM 0
Share

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.

avatar image Jens T Jens T · Jul 20, 2010 at 09:09 AM 0
Share

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

avatar image
0

Answer by fil · Oct 17, 2012 at 12:02 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Calculate distance between two objects 6 Answers

Can I get a warning if I (involuntarily) declare a new js variable in an assignment? 2 Answers

Display the speed of a Game Object in MPH 1 Answer

How can I wait for Unity to recompile during the execution of an editor script? 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges