Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Error when building: "Type or Namespace could not be found"

Discussion in 'Editor & General Support' started by Mortilicious, Oct 9, 2023.

  1. Mortilicious

    Mortilicious

    Joined:
    Oct 15, 2021
    Posts:
    7
    I am trying to create a build of my current game. Everything seems to be ok, but when I press "Build" on the game, I get a set of errors that state "Type of Namespace could not be found".

    errors.PNG

    All the errors seem to come from the same script, see below. Double clicking on the errors seem to send me to the first sentence of the script where the class is declared.

    What I tried: When I search for this error online, all problems I find seem to go wrong in the editor itself (throwing the error as people are typing). However, for me the editor shows 0 errors. What is more, the script that is throwing the errors is a script I found on a forum online to help me make the editor a bit more accessible, allowing the showing of fields conditionally based on the choices made in other fields.

    As this is an editor-only script, I don't even really need it to build the game, as the editor won't be visible.

    How could I solve the setup so the game successfully builds?

    Thanks in advance!

    The script in question is as follows:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Based on: https://forum.unity.com/threads/draw-a-field-only-if-a-condition-is-met.448855/
    6. /// </summary>
    7. [CustomPropertyDrawer(typeof(DrawIfAttribute))]
    8. public class DrawIfPropertyDrawer : PropertyDrawer
    9. {
    10.     #region Fields
    11.  
    12.     // Reference to the attribute on the property.
    13.     DrawIfAttribute drawIf;
    14.  
    15.     // Field that is being compared.
    16.     SerializedProperty comparedField;
    17.  
    18.     #endregion
    19.  
    20.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    21.     {
    22.         if (!ShowMe(property) && drawIf.disablingType == DrawIfAttribute.DisablingType.DontDraw)
    23.             return 0f;
    24.  
    25.         // The height of the property should be defaulted to the default height.
    26.         return base.GetPropertyHeight(property, label);
    27.     }
    28.  
    29.     /// <summary>
    30.     /// Errors default to showing the property.
    31.     /// </summary>
    32.     private bool ShowMe(SerializedProperty property)
    33.     {
    34.         drawIf = attribute as DrawIfAttribute;
    35.         // Replace propertyname to the value from the parameter
    36.         string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName;
    37.  
    38.         comparedField = property.serializedObject.FindProperty(path);
    39.  
    40.         if (comparedField == null)
    41.         {
    42.             Debug.LogError("Cannot find property with name: " + path);
    43.             return true;
    44.         }
    45.  
    46.         // get the value & compare based on types
    47.         switch (comparedField.type)
    48.         { // Possible extend cases to support your own type
    49.             case "bool":
    50.                 return comparedField.boolValue.Equals(drawIf.comparedValue);
    51.             case "Enum":
    52.                 return comparedField.enumValueIndex.Equals((int)drawIf.comparedValue);
    53.             default:
    54.                 Debug.LogError("Error: " + comparedField.type + " is not supported of " + path);
    55.                 return true;
    56.         }
    57.     }
    58.  
    59.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    60.     {
    61.         // If the condition is met, simply draw the field.
    62.         if (ShowMe(property))
    63.         {
    64.             EditorGUI.PropertyField(position, property);
    65.         } //...check if the disabling type is read only. If it is, draw it disabled
    66.         else if (drawIf.disablingType == DrawIfAttribute.DisablingType.ReadOnly)
    67.         {
    68.             GUI.enabled = false;
    69.             EditorGUI.PropertyField(position, property);
    70.             GUI.enabled = true;
    71.         }
    72.     }
    73.  
    74. }
     
  2. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    284
    This property drawer script needs to be in an editor-only assembly. You either need to put it in a directory named "Editor" or in an assembly definition that is marked for inclusion in the editor only. When you try to make a build, it's trying to include this property drawer script in your released game, but the built executable doesn't include a bunch of editor only stuff that it doesn't need to run the built version of the game. So, your property drawer suddenly can't find the editor references it needs when trying to include it in your built game. So, the trick is to just not include this script in your built game.
     
    Mortilicious likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    (Building on top of what CodeRonnie says above, which is 100% correct...)

    This error is reported in scripts that use classes within the
    UnityEditor
    namespace.

    You may only use
    UnityEditor
    namespace methods and classes while you are in the Unity editor.

    You may never use anything from the
    UnityEditor
    namespace in a build.

    You can fix this in one of two ways:

    - wrap these bits of editor code in
    #if UNITY_EDITOR
    conditional compilation directives

    - you can put the entire script into a folder called
    Editor
    . This makes it what is loosely called an "editor script."

    Such "editor scripts" can NEVER be attached to GameObjects in either prefabs or scenes.
     
  4. Mortilicious

    Mortilicious

    Joined:
    Oct 15, 2021
    Posts:
    7
    This fixed the issue. I moved the script to a new folder at root level called Editor and it built as expected. Thanks both!
     
    CodeRonnie likes this.