Search Unity

Problem with UnityEditor

Discussion in 'Scripting' started by DarkSoul, May 15, 2010.

  1. DarkSoul

    DarkSoul

    Joined:
    Dec 24, 2009
    Posts:
    37
    I want add a new tag in the Unity editor from script, but give me this error:

    Code (csharp):
    1. Assets/Logic/WayPoints/WayPoint.cs(3,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
    What is the problem?
     
  2. rpgrichard

    rpgrichard

    Joined:
    Oct 28, 2009
    Posts:
    8
    You haven't posted your WayPoint.cs script, so I don't know what function you are trying to call.

    My educated guess is that you are trying to use an object whose class is part of UnityEditor.

    To use a UnityEditor class/object you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.

    I hope this helps.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    your educated guess is right according the path the error shows.

    UnityEditor is only available to classes that are within an "Editor" folder
     
  4. DarkSoul

    DarkSoul

    Joined:
    Dec 24, 2009
    Posts:
    37
    Ah, thanks!, the problem was the folder. I did not know that the script was saved in the Editor folder.

    Thank you very much.
     
  5. Ailos

    Ailos

    Joined:
    Sep 14, 2011
    Posts:
    13
    But how can I reference a script , that is IN the Editor folder , I mean.

    ScriptName sc;

    I alwais obtain this

    The type or namespace name `ScriptName ' could not be found. Are you missing a using directive or an assembly reference?
     
  6. wheats

    wheats

    Joined:
    Aug 3, 2011
    Posts:
    14
    Same here I saw on another post that the reason for javascript not working on path objects was because my scripts were in the wrong place . I guess I must have just moved the editor scripts at the same time.

    Thanks
     
  7. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I don't believe that is technically true. My scriptable object uses the UnityEditor directive and is able to implement its own OnInspectorGUI method, which allows me to call this method anywhere I want the scriptable object inspector to be shown.

    The only problem is when you try to build the game you'll get errors, because UnityEditor cannot be included in a standalone/webplayer,/etc. build. However, I have found that using platform dependent compilation fixes the problem.

    Simply put
    Code (csharp):
    1.  #if UNITY_EDITOR
    before the using UnityEditor directive and
    Code (csharp):
    1. #endif
    after it. You will also need to surround your GUI code with these same tags.

    Hope this helps!

    Edit: Be careful if surrounding serialized fields with this, as Unity will throw errors when trying to build.
     
    Last edited: May 6, 2017
  8. HamFar

    HamFar

    Joined:
    Nov 16, 2014
    Posts:
    89
    Guys, Does this mean that I can never have a specific Editor functionality in a build?
    I have a script that uses radio buttons, so I need to add the following as a directive:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. #endif
    But I need the radio buttons to work when I run my app in standalone, and I cannot simply move my script into the Editor folder.
    How do I resolve this?
     
  9. DevSpur

    DevSpur

    Joined:
    Jul 19, 2014
    Posts:
    20
    Hey I Had the same problem and the way that i got it to work all i did was i add "Using system" at the top of both scripts then you can have both scripts in a scripts folder