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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Script can't find my CustomAttribute namespace

Discussion in 'Editor & General Support' started by NamelessGames_, Jan 20, 2023.

  1. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    31
    I've this script
    Code (csharp):
    1. using UnityEngine;
    2.  
    3.     public class Health : MonoBehaviour
    4.     {
    5.         [SerializeField] float _maxValue = 40f;
    6.         [SerializeField, ReadOnly] float _currentValue = 40f;
    7.     }
    and this
    Code (csharp):
    1. PropertyAttribute
    2.     using UnityEngine;
    3.  
    4.     public class ReadOnlyAttribute : PropertyAttribute
    5.     {
    6.     }
    but VS and Unity said "Name or namespace could not be found".
    I tried much solutions given in other questions, nothing worked. Can you help me?

    -- Edit

    This is my folder tree:
    /Assets/Scripts/UnitComponents/Health.cs
    /Assets/Scripts/Editor/Attributes/ReadOnlyAttribute.cs
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Unity compiles scripts into default assemblies if you don't define your own, the main two being Assembly-Csharp and Assembly-Csharp-Editor. Files in folders with 'Editor' are compiled into the latter; read more here: https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    Two assemblies can't each reference each other, only in one direction, so the editor assembly references the regular one. Thus, the normal one can't know anything about the editor assembly.

    Your attribute will be in the regular assembly, just like Unity's attributes are as well.
     
    NamelessGames_ likes this.
  3. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    31
    Oh... I feel so dumb... I was going for Attributes like I usually go for Drawers and Editors...

    Thank you so much!
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    No problem! Easy mistake to make the first time.

    You can decorate attributes with
    [System.Diagnostics.Conditional("UNITY_EDITOR")]
    to prevent them being compiled into the built game, which I use for most of my attributes.
     
    NamelessGames_ likes this.
  5. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    31
    Yeah, for sure. Only it was not the first time XD.

    Ty for Decorate, I didn't know it!