Can't add layout groups via script - Get a null ref exception from HorizontalOrLayoutGroup.CalcAlong

Hi folks,

I’m attempting to create my UI in code, and have run into an…interesting problem.

I’ve found that when I attach a horizontal or vertical layout group via AddComponent, I get the following:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UI.HorizontalOrVerticalLayoutGroup.CalcAlongAxis (Int32 axis, Boolean isVertical) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Layout/HorizontalOrVerticalLayoutGroup.cs:16)
UnityEngine.UI.VerticalLayoutGroup.CalculateLayoutInputHorizontal () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Layout/VerticalLayoutGroup.cs:12)
UnityEngine.UI.LayoutRebuilder.<Rebuild>m__8 (UnityEngine.Component e) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs:70)
UnityEngine.UI.LayoutRebuilder.PerformLayoutCalculation (UnityEngine.RectTransform rect, UnityEngine.Events.UnityAction`1 action) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs:133)
UnityEngine.UI.LayoutRebuilder.Rebuild (CanvasUpdate executing) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs:70)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:122)
UnityEngine.Canvas:SendWillRenderCanvases()

Through the process of (commenting out) elimination I’ve found this only happens when adding the layout group, regardless of setting values to it or not.

protected VerticalLayoutGroupPanelObject CreateVerticalLayout(HorizontalOrVerticalLayoutPanelFactoryOptions options)
{
    //I've got an inherited chain of factories to make creating UI elements easy. I've verified creation of
    //the image panel is not the problem (it's just a game object with a rect transform, canvas renderer, and image component
    var imagePanelObject = base.CreateImagePanel(options);
    var resultObject = VerticalLayoutGroupPanelObject.ConvertFromImagePanelObject(imagePanelObject);
    resultObject.Panel.SetActive(true);
    //Horizontal Layout
    //Comment this out, null ref goes away. Leave it here, null ref slammed in my face >.<
    var layout = resultObject.Panel.AddComponent<VerticalLayoutGroup>();
    //Extension method that sets it's variables. Closest I can get to DI w/ monobehaviors.
    //Tried commenting it out as well to no effect, get a null ref either way once I run the scene.
    layout.Initialize(options.HorizontalVerticalConfig);
    resultObject.VerticalLayoutGroupComponent = layout;

    return resultObject;
}

and the options (though even when not set, bad things happen).

private PlayerMenuContainerFactoryOptions CreateContainerOptions(Transform parent)
{
    var options = new PlayerMenuContainerFactoryOptions();
    //game object
    options.GameObjectConfig = new GameObjectConfigurationOptions();
    options.GameObjectConfig.DoesWorldTransformStay = false;
    options.GameObjectConfig.Layer = LayerMask.NameToLayer(Layers.UI);
    options.GameObjectConfig.Name = "Player Menu Container";
    options.GameObjectConfig.Parent = parent;
    //rect - anything not explicitly set defaults to 0
    options.RectConfig = new RectTransformConfigurationOptions();
    options.RectConfig.AnchorMax = new Vector2(1, 1);
    options.RectConfig.Pivot = new Vector2(0.5f, 0.5f);
    //image
    options.ImageConfig = new ImageConfigurationOptions();
    options.ImageConfig.SourceImage = this.spriteCacher.Fetch("panel_brown");
    options.ImageConfig.ImageColor = Color.white;
    options.ImageConfig.UseRaycastTarget = true;
    options.ImageConfig.ImageType = Image.Type.Sliced;
    options.ImageConfig.IsFillCenter = true;
    //vertical conifg
    options.HorizontalVerticalConfig = new HorizontalOrVerticalLayoutGroupConfigurationOptions();
    options.HorizontalVerticalConfig.ChildAlignment = TextAnchor.UpperLeft;
    options.HorizontalVerticalConfig.IsForceChildHeightExpand = true;
    options.HorizontalVerticalConfig.IsForceChildWidthExpand = true;

    return options;
}

Everything looks right in the inspector, however children of a layoutgroup object never recognize they’re in a layout (probably on account of the null ref exception). I’ve commented out all the children code, and have just a canvas with a horizontal image panel being added to said canvas.

I’m stumped as I’m not sure at all what could even cause CalcAlongAxis to throw a null ref, or why simply adding a layout group component would cause it.

Does anyone have any ideas? Any help would be greatly appreciated.

Whew, found the issue after getting a good nights rest.

Padding (or rather RectOffset) is a class, not a struct and if not specified was defaulting to null.