Auto apply a component to a public variable

I have a class with

    [RequireComponent(typeof(Text))]
    public class MyClass : MonoBehaviour
    {
        public Text _text = null;
    }

it’s possible to apply this “required component” to my public Text variable when i add the script to a gameobject?

Have you tried this already?

void OnValidate()
{
   _text = GetComponent<Text>();
}
2 Likes