I am trying to create a custom slider to use in my project at runtime in a HUD that is always on the screen, because the styling options for the default slider are very limited. After many attempts on my own, google searches, Documentation/Manual reviews, etc…I found a tutorial (Official Unity – I prefer those) on building a custom toggle switch, because it was closest to the slider (might be a bad assumption, but it has helped me understand the process of creating a custom control in C#). Link to the tutorial.
So in the hierarchy in the UI Builder, the control has the following Heirarchy:
SlideToggle (This is the custom control based on BaseField)
Label
VisualElement (the toggle field which contains the knob)
VisualElement (the knob)

The tutorial took me through the process of building the custom uss file and the C# class for the new SlideToggle. It seems trivial to access the VisualElements and make changes to their classes.
However, when I try to access the Label, to change the margins, padding, size, etc. with a custom class, for the life of me, none of the queries I have tried work.
In C# this works perfect to change the styling of the Visual Elements:
using UnityEngine;
using UnityEngine.UIElements;
namespace MyUILibrary
{
// Derives from BaseField<bool> base class. Represents a container for its input part.
public class SlideToggle : BaseField<bool>
{
public new class UxmlFactory : UxmlFactory<SlideToggle, UxmlTraits> { }
public new class UxmlTraits : BaseFieldTraits<bool, UxmlBoolAttributeDescription> { }
// In the spirit of the BEM standard, the SlideToggle has its own block class and two element classes. It also
// has a class that represents the enabled state of the toggle.
public static readonly new string ussClassName = "slide-toggle";
public static readonly new string inputUssClassName = "slide-toggle__input";
public static readonly string inputKnobUssClassName = "slide-toggle__input-knob";
public static readonly string inputKnobCheckedUssClassName = "slide-toggle__input--checked";
public static readonly new string labelUssClassName = "slide-toggle__label";
public static readonly string baseFieldLabelClassName = "unity-base-field__label";
VisualElement m_Input;
VisualElement m_Knob;
VisualElement m_Label;
// Custom controls need a default constructor. This default constructor calls the other constructor in this class.
public SlideToggle() : this(null) { }
// This constructor allows user to set the contents of the label
public SlideToggle(string label) : base(label, null)
{
// Style the control overall
AddToClassList(ussClassName);
// Get the BaseField's visual input element and use it as the background of the slide.
m_Input = this.Q(className: BaseField<bool>.inputUssClassName);
m_Input.AddToClassList(inputUssClassName);
Add(m_Input);
//For the life of me I cannot find a query which will access the Label
m_Label = this.Q(className: BaseField<bool>.labelUssClassName);
if(m_Label is null)
{
Debug.Log("m_Label was not assigned.");
}
else
{
Debug.Log($"m_Label = {m_Label}");
}
// Create a "knob" child element for the background to represent the actual slide of the toggle.
m_Knob = new();
m_Knob.AddToClassList(inputKnobUssClassName);
m_Input.Add(m_Knob);
// The code continues from here to implement the functions and callbacks
However, as noted int the code comment at the point in the code where I try to find the Label so I can add my custom styling, I cannot find a query that works.
The one in the code seems the most intuitive, but I have also tried:
m_Label = this.Q<Label>();
m_Label = this.Q(className: ussClassName).Children().GetEnumerator().Current;
m_Label = this.Q(className: ussClassName).Children().First();
This last one won’t even compile even though is seems to be correct according to this documentation.
I have tried others, but I cannot remember them.
NOTE: I included the if - then block because if you try to do anything with m_Label when it is null, the console logs an endless loop of NullReference errors. (I reported that bug separately).
Please let me know if I am simply overlooking something or if there is a reason I cannot access the label to change it’s styling.