Most efficient way to check if a component is null for a canvas or UI?

Hi, so is pretty simple, I got the following code and I want to know if there’s a more efficient / compact way of doing this. Thank you!

public class ShapesController : MonoBehaviour
{
    [Header("Canvas Groups")]
    [SerializeField] CanvasGroup rectangleCanvas;
    [SerializeField] CanvasGroup circleCanvas;
    [SerializeField] CanvasGroup triangleCanvas;
    [Header ("Rectangle Input Fields")]
    [SerializeField] InputField rectangleXOneInput;
    [SerializeField] InputField rectangleYOneInput;
    [SerializeField] InputField rectangleXTwoInput;
    [SerializeField] InputField rectangleYTwoInput;
    [Header("Circle Input Fields")]
    [SerializeField] InputField circleXInput;
    [SerializeField] InputField circleYInput;
    [SerializeField] InputField circleRadiusInput;
    [Header("Triangle Input Fields")]
    [SerializeField] InputField triangleXAInput;
    [SerializeField] InputField triangleYAInput;
    [SerializeField] InputField triangleXBInput;
    [SerializeField] InputField triangleYBInput;
    [SerializeField] InputField triangleXCInput;
    [SerializeField] InputField triangleYCInput;
    void Start()
    {
        prepare();
    }

    void prepare()
    {
        if (rectangleCanvas == null) rectangleCanvas.GetComponent<CanvasGroup>();
        if (circleCanvas == null) circleCanvas.GetComponent<CanvasGroup>();
        if (triangleCanvas == null) triangleCanvas.GetComponent<CanvasGroup>();
        if (rectangleXOneInput == null) rectangleXOneInput.GetComponent<InputField>();
        if (rectangleYOneInput == null) rectangleYOneInput.GetComponent<InputField>();
        if (rectangleXTwoInput == null) rectangleXTwoInput.GetComponent<InputField>();
        if (rectangleYTwoInput == null) rectangleYTwoInput.GetComponent<InputField>();
        if (circleXInput == null) circleXInput.GetComponent<InputField>();
        if (circleYInput == null) circleYInput.GetComponent<InputField>();
        if (circleRadiusInput == null) circleRadiusInput.GetComponent<InputField>();
        if (triangleXAInput == null) triangleXAInput.GetComponent<InputField>();
        if (triangleYAInput == null) triangleYAInput.GetComponent<InputField>();
        if (triangleXBInput == null) triangleXBInput.GetComponent<InputField>();
        if (triangleYBInput == null) triangleYBInput.GetComponent<InputField>();
        if (triangleXCInput == null) triangleXCInput.GetComponent<InputField>();
        if (triangleYCInput == null) triangleYCInput.GetComponent<InputField>();
    }
}

Okay, I think I see what you’re trying to do here however if any of your components are null you will get exceptions.

Say rectangleCanvas is null, the if-statement body gets executed and so we call “rectangleCanvas.GetComponent();”.

However we know rectangleCanvas is null… So we will have a null reference exception (or missing object exception, however unity wraps it up) incoming.

You will need to find your components based on some other criteria as if it’s null then you can’t use it to find itself! Maybe they have certain components on or are called something specific?

So for the slightly better news - I expect you’re trying to populate your serialized field references from code and you can do this entirely at edit-time so there won’t be any overhead from searching for components at runtime when you start the game or instantiate objects. The OnValidate function is ran whenever we change a value in the inspector or when a component is loaded in the editor so is perfect for populating references.

#if UNITY_EDITOR
private void OnValidate()
{
    if (rectangleCanvas == null)
    {
        rectangleCanvas = ...
        // insert your code to find your rectangle canvas.
    }
}
#endif

I expect in this case it might be easier to drag your references in by hand but OnValidate is there for you if you need to do this via code :slight_smile: Let me know if you need any clarification on any of this.