Posts Tagged Unity3D

Unity 3D JavaScript and C# Scoping/Compiling Issue

This is a quick write-up regarding something as simple as getting JavaScript to interoperate properly with C# scripts in your Unity projects.

I ran into such an issue after purchasing EZ Gui via the Unity Asset Store.  Immediately upon purchasing you are prompted to “import” the project into your codebase.  I did this without issue.  Everything worked fine and dandy until my JavaScript code required referencing a SpriteText object.  This is when the following issue began happening:

Unity BCE0018: The name ‘SpriteText’ does not denote a valid type (‘not found’).

My folder structure was simply the following:

/scripts/myCameraScript.js  <– trying to reference the SpriteText

 

View Code JAVASCRIPT
var test : SpriteText;  //this line was throwing the error.

 

/EZ GUI/ <– default after purchasing and importing from the Unity Asset store.

I sat and read Unity compiling and scoping rules over and over located here.  On point 4, the statement reads “All scripts that are compiled in this step have access to all scripts in the first group (“Standard Assets”, “Pro Standard Assets” or “Plugins”).”

The solution is simple here. Take the EZ Gui code located in the “Plugins” folder and move it to the root.

/EZ Gui/Plugins/ <– move this to:

/Plugins/

Done. It works. Super simple.

Bonus Play, Rule of thumb (kind of no brainers really):

  1. Place all your plugins in the /Plugins/ folder.
  2. Place all your C# Code into the /Standard Assets/ folder.
  3. Place all your JavaScript code into /Scripts/ or any folder that is NOT named (“Standard Assets”, “Pro Standard Assets” or “Plugins”)

This will lead to success and less headaches.

, , , , ,

No Comments