Can I change variable name on inspector?

Hello everyone,

I was wondering if can we change the variable name on inspector without changing it in script.

For example:

using UnityEngine;
using System.Collections;
  
public class Luces : MonoBehaviour
{	
    [Header("Luces")]
    public Light luzRoja;
}

Instead of saying “Luz Roja” on inspector, I want to say “Roja”.

I have more code, but this is what I see now:

dEJRZAk

Thanks !

ANSWERED:

Thanks everyone for the answers. I knew that maybe the best options was to create a custom inspector, but for this, is a lot of work and makes no sense… So, it will be Luz Roja :stuck_out_tongue:

You can do this with PropertyAttribute:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LabelOverride : PropertyAttribute
{
    public string label;
    public LabelOverride ( string label )
    {
        this.label = label;
    }
  
    #if UNITY_EDITOR
    [CustomPropertyDrawer( typeof(LabelOverride) )]
    public class ThisPropertyDrawer : PropertyDrawer
    {
        public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
        {
            try
            {
                var propertyAttribute = this.attribute as LabelOverride;
                if( IsItBloodyArrayTho( property ) == false )
                {
                    label.text = propertyAttribute.label;
                    
                } else
                {
                    Debug.LogWarningFormat(
                        "{0}(\"{1}\") doesn't support arrays ",
                        typeof(LabelOverride).Name ,
                        propertyAttribute.label
                    );
                }
                EditorGUI.PropertyField( position , property , label );
            } catch ( System.Exception ex ) { Debug.LogException( ex ); }
        }
        
        bool IsItBloodyArrayTho ( SerializedProperty property  )
        {
            string path =  property.propertyPath;
            int idot = path.IndexOf('.');
            if( idot==-1 ) return false;
            string propName = path.Substring( 0 , idot );
            SerializedProperty p = property.serializedObject.FindProperty( propName );
            return p.isArray;
            //CREDITS: https://answers.unity.com/questions/603882/serializedproperty-isnt-being-detected-as-an-array.html
        }
    }
    #endif
}

So you can easily override inspector labels:

[LabelOverride( "Awesome light" )]
[SerializeField]
Light _light;

To see what real name of this field is you must switch Inspector to Debug mode.

If you don’t want to write your own PropertyDrawer, you can use NaughtyAttributes.Label:

public class Luces : MonoBehaviour
{
    [Header("Luces")]
    [Label("Roja")]
    public Light luzRoja;
    // ...

NaughtyAttributes is open source and available as a package.

If it could be done, it would have to be through serialization. The thing is that it is not a good idea to have a variable name for a script variable because, say that you want to access that variable through another script. Unity wouldn’t know what to access once it finds a variable named ‘Roja’ because in the script it is called ‘Luz Roja’ so it won’t make the connection.

I recommend changing it in the script if you don’t want headaches.

Best regards and Good Luck!

  1. I would recommend against changing the field names as they show up. The big reason (there are others), is think of anytime you have tried to GetComponent<…>() and couldn’t find fields that matched the UI names. It makes it difficult to develop against in the future by you or others.

  2. A common trick I do to help me organize my script fields, is inside of my MonoBehaviour, I create a small serialized class called (to mimic your system) public class LucesFields. Then I have fields for each one, such as public Light Roja; public Light Verde; etc. Then I create a public field call like this: public LucesFields Luces;

This gives you the header, but also makes the header collapsable. you can name stuff more simply, because if you have Roja field for a UI Slider and a Light, then the header separates it for simplicity. This also reduces junk in Intellisense. Instead of having to find LucesRoja in all of the members in a monobehaviour, you just type Luces (for the lights) then “.R” and intellisense will allready be simplified. I think this might be the better practice.

Don’t get me wrong, I initially loved the helper attribute that @Hunter_Towe suggested above. But after consideration, the fact that it adds a “Magic” layer that hides how to find the variable in the code, I couldn’t agree with it. Pretty, but the type of thinking that will help make code more complicated in the long run. (I mean no offense by that, I have had hundreds of instances like that, and I’m still catching myself doing things like these days)