0
\$\begingroup\$

I know that certain namespaces are necessary to include to specify under what scope certain methods are found, and this also saves typing. Otherwise the error CS0246 is thrown if referencing an unknown type.

The problem is, that I am using a namespace in a script in:

 Assets / Scripts

And that namespace is defined in a script in:

 Editors / Data / AnotherFile

So I am getting an error CS0246.

How could I solve this without moving that script into the Assets / Scripts location?

SCRIPT LOCATED IN EDITOR / DATA / ANOTHERFILE:

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace NodeEditorWindow
{

[CreateAssetMenu]
public class LevelFlowData : BaseNodeEditorData<BaseLevelNode, LevelFlowEditorAttributes>
{
    private const string MENU_ITEM_PATH = "Create / ScriptableObjects / LevelFlowData";
    private const string NEW_ASSET_PATH = "ScriptableObjects / Level Flow Data";

    [SerializeField] private string _name;

    /// <summary>Gets and Sets name property.</summary>
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }

    [MenuItem(MENU_ITEM_PATH)]
    public static void CreateAsset()
    {
        LevelFlowData scriptableObject = ScriptableObject.CreateInstance<LevelFlowData>() as LevelFlowData;
        AssetDatabase.CreateAsset(scriptableObject, AssetDatabase.GenerateUniqueAssetPath(NEW_ASSET_PATH));
    }
}

}

SCRIPT LOCATED ON ASSETS / SCRIPTS / ...:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GameData;
using NodeEditorWindow; //HERE I AM USINGTHE OTHER CLASS'S NAMESPACE

public class LevelController : Singleton<LevelController>
{
    [Header("Level Data:")]
    [SerializeField] private LevelFlowData levelFlowData;
}

These are the errors I get in console (both CS0246):

Assets/Scripts/Scene Controllers/LevelController.cs(6,7): error CS0246: The type or namespace name `NodeEditorWindow' could not be found. Are you missing an assembly reference?

Assets/Scripts/Scene Controllers/LevelController.cs(11,27): error CS0246: The type or namespace name `LevelFlowData' could not be found. Are you missing an assembly reference?

Sorry if this seems to be a repeated question, but I haven't found any solution to this error due to having scripts on different paths (but using the namespaces). Thanks in advance.

\$\endgroup\$
1
  • \$\begingroup\$ Seems like it should work to me. Have you tried restarting the editor? Intellisense does get drunk sometimes... \$\endgroup\$ Oct 23, 2017 at 3:43

1 Answer 1

2
\$\begingroup\$

The problem is that you're including editor code into the "runtime" code. Unity places the editor scripts in a different assembly, so basically your editor code is not accessible from the actual game code. You need to move your editor script and its parent classes) from the Editor folder and you can do something like

#if UNITY_EDITOR
using UnityEditor;
/*
  Editor code
*/
#endif

which should give you access to Unity's Editor API. In this way you're "simulating" the build-time stripping of editor code and still be able to use runtime scripts alongside editor scripts. It's a bit of a headache, especially if you're using a fair amount of custom editor scripts, which all need to be flipped to the runtime assembly.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If memory serves, I don't think adding using UnityEditor alone is enough. That just gets access to Unity's built-in editor code, not the custom editor scripts we've defined. They're still built into a separate assembly, after the non-editor scripts (so that custom editor scripts can reference the custom gameplay scripts they act on). So far as I'm aware, only scripts under a folder named "Editor" can reference other custom editor scripts. \$\endgroup\$
    – DMGregory
    Oct 23, 2017 at 13:29
  • 1
    \$\begingroup\$ You're right, my answer was incomplete, because the modification was supposed to be applied to the actual editor scripts, not the runtime script itself. I've updated the answer with the new info. \$\endgroup\$
    – Hyperg
    Oct 23, 2017 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .