- Home /
Why can't some of my scripts see other scripts or namespaces?
I'm creating a custom editor for an item system, and for some reason, some of my scripts cannot access other scripts or namespaces. What would be causing this?
// This script can access any other script
// File located in Assets/ItemSystem/Scripts/Editor/ISEditor
namespace ItemSystem.Editor {
public class ItemDatabaseType<D,T> where D : ItemDatabase<T> where T: ISObject
{
ISObject item; // Works fine
}
}
// This script's access is very limited. Can't access the above namespace
// or script (even if script is in same namespace).
// File located in: Assets/ItemSystem/Scripts/ISObjects
using ItemSystem.Editor // Doesn't work
namespace ItemSystem {
public class ISObject
{
OtherISObject otherItem; // Works fine
ItemSystem.OtherISObject otherItem2; // Works fine
// These do not work:
ItemDatabaseType<DatabaseScript, ISObject > idt;
ItemSystem.Editor.ItemDatabaseType<DatabaseScript, ISObject > idt2;
// Error: The type or namespace could not be found,
// though DatabaseScript and ItemScript are.
}
}
// This script has no namespace. It can access the ItemSystem namespace, but not ItemSystem.Editor
// File located in Assets/ItemSystem/Scripts/DatabaseScripts
public class ItemDatabase<T> : ScriptableObject where T: ItemSystem.ISObject {
ItemSystem.ISObject item; // Works
ItemSystem.Editor.ItemDatabaseType<DatabaseScript, ISObject> item2; // Doesn't work
}
how about declare it before? like this :
using UnityEngine;
using System.Collections;
using TestNameSpace; //FIX$$anonymous$$E comment here to see the change below
using TestNameSpace.SubNameSpace; //FIX$$anonymous$$E comment here to see the change below
namespace TestNameSpace{
public class TestClass{
int i;
public TestClass(){
i = 1;
}
}
}
namespace TestNameSpace.SubNameSpace{
public class SubTestClass{
int i;
public SubTestClass(){
i = 2;
}
}
}
public class Test : $$anonymous$$onoBehaviour {
TestClass testClass; //see change here
SubTestClass subTestClass; //see change here
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I did initially test that, and it didn't work. As I stated in the accepted answer, the problem was caused by scripts being placed in an "Editor" folder. If "SubNameSpace" is only referenced in the "Editor" folder, then it doesn't exist; and if it is used outside of the folder, then only the files outside are visible. So far, there seems to be no way to access Editor scripts from the outside. If there is code specific to the Editor, then you can use #if UNITY_EDITOR & #endif. The two issues with that are 1) You have to put these two lines around every chunk of code that is only used by the Editor, and 2) I don't know if the code is still compiled (just not used) by Unity in the final build. For more information, check out: http://docs.unity3d.com/$$anonymous$$anual/SpecialFolders.html http://docs.unity3d.com/$$anonymous$$anual/ScriptCompileOrderFolders.html
public class TestClass {
void Some$$anonymous$$ethod() { }
#if UNITY_EDITOR // Code used only if Editor is present
void EditorOnly$$anonymous$$ethod() { }
#endif
}
I think preprocessor directives strips codes out, so the codes does not being compiled at all when the build happens, isn't it? And the Editor-only scripts should not be included to the final build, so using #if UNITY_EDITOR is necessary and I didn't find it annoying.
Answer by Ryaurezh · Dec 30, 2015 at 06:58 AM
I figured it out. The issue is caused by placing some of my scripts in a folder called "Editor", which allows easy access to the UnityEditor, plus they are not included in the final build. The downside is that those scripts are not detected by others outside of the folder because Unity compiles them last; so as far as other scripts are concerned, they don't exist.
Answer by BranchOffGameStudio · Jan 05, 2018 at 01:39 AM
I had the same issue. My Editor scripts were not able to recognize other script's/namespaces outside of the Editor folder. In order to fix I did the following:
Move the editor scripts out of the editor folder into a folder with the rest of my scripts.
Added the namespace to my editor scripts
Closed monodevelop.
Moved my editor scripts back into the Editor folder.
Opened mono and built the project.
Now my project has namespaces for my Scriptable Objects, MonoBehaviours and Editor Scripts! Monodevelop can be fickle sometimes, but a little bit of persistence and toying can work to your advantage.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054326im_/https://answers.unity.com/themes/thub/images/avi.jpg)