Is there a way to set a custom name to a component in the inspector?

I have a class that I use to do different things.
I would like to know if there is any way to name it and differentiate it at a glance.

This is my code:

//-------------------------------------------------------
using TransformExtensionMethods;
using UnityEngine;
//-------------------------------------------------------
public class HorizontalDetector : MonoBehaviour
{
    //-------------------------------------------------------
    [SerializeField] float rayCastDistance = 5f;
    [SerializeField] LayerMask layerToDetect;
    [SerializeField] private float offsetX = 0f;
    [SerializeField] private float offsetY = 0f;
    RaycastHandler ray;
    //-------------------------------------------------------
    void Awake() => ray = new RaycastHandler();
    //--------------------------------------------------------
    public bool IsDetected()
    {
        if (ray == null) return false;
        Transform transform1;
        Vector3 direction = (transform1 = transform).GetDirection();
        var position = transform1.position;
        Vector2 origin = new Vector2(position.x + offsetX, position.y + offsetY);
        return ray.IsHitting(origin, direction, rayCastDistance, layerToDetect);
    }
    //-------------------------------------------------
    public Vector3 GetPoint()
    {
        return ray.HitPoint();
    }
    //-------------------------------------------------
}

I am looking for something like this:

public class HorizontalDetector : MonoBehaviour
{
    [SetNameFromEditor("  ")]
    //or
    [SetLabel(" ")]
    //or
    [SetCaption(" ")]
    
}

I’ve thought of just putting a string at the beginning and serializing it. Something like this.

public class HorizontalDetector : MonoBehaviour
{
   [SerializeField] string ComponentName;
}

But that would be a variable that I would not use in the computation and would occupy memory.

And maybe there is something to do what I’m looking for, I still don’t know.

Is there a way to custom name a component?

Thank you so much!!

1 Like

Nope. It is not possible and it would make serialization impossible. You can inherit from this base class to more specialized ones and use those in your project. It is a good idea anyway to use this kind of very generic classes as services or bases and use specialized ones to interact with.

Alternatively you can use the Odin asset (paid), it has some capabilities to put custom labels in your inspector without writing custom inspector code. But research this well, because that solution may not suit your case, they are using attributes for it with some logic capabilities.

1 Like

Okay, thanks for your help!!

Having recently purchased Odin Inspector, I have been able to make a title that’s based on a Scriptable Object’s name to make it easier to read in the editor. You can use expressions to call a Property or Method that returns a string. I simply had a Property that returned ā€˜this.name’ used by a label attribute.

The same should be possible in a monobehavior too.

1 Like

This ^ ^ ^ and you can even inherit from your base class and provide nothing new at all in the derived class. The ā€œempty offspringā€ of your base class will still be treated as completely different classes, if you only want the ā€œtypingā€ of it.

1 Like

Thanks for your answers gentlemen