Get a scrollRect to begin with the top of content aligned to top of scrollrect pane.

I created a scrollRect for a menu full of buttons.

The number of buttons dynamically changes based on which one is pressed.

There could be as few as 3 buttons or as many at 20.

When there are a greater number of buttons than will fit withing the scrollrect pane mask they can be scrolled by dragging the mouse up and down on the pane.

The buttonpane containing the buttons has a layout element and a grid layout script attached.

The parent content pane has a content size fitter script AND a layout element script and a vertical layout group attached.

The parent of the content pane gameobject is a gameobject that has a scrollrect attached and an image and a mask.

The parent of the scrollrect gameobject is the canvas and has an image attached.

The size of the buttonpane stretches vertically when more buttons are added (when the menu changes).

How to I get the content pane top bound to be aligned with the scrollrect objects top bound whenever the content pane’s number of buttons changes?

I could add a code example but because the whole structure was created and initialized in code and the number of buttons within the grid layout group are enabled/disabled by a lot of other code it would probably be more of a hindrance than a help as it numbers over 500 lines.

The problem is I do not know how many buttons will be in the menu at any one time the contentpane size is driven automatically by the number of buttons currently enabled in the buttonpane child object.

I’m looking for some kind of function that will align the top of one rect transform to the top of another rect tranform in c#.

If the goal is to have the buttons fill in from the top, this can be achieved by setting the anchor and pivot of the content pane to be the top of parent gameobject that has the scrollrect. The live training video shows how this is done:

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-scroll-lists-at-run-time

and the Hints section of the scrollrect manual page references this as well:

http://docs.unity3d.com/Manual/script-ScrollRect.html

In the video, because the content pane is stretched in both directions to start, the default behavior is to fill in from the middle, since this is where the pivot is. If you change its y anchors to be ymin: 1; ymax; 1 and the pivot to y: 1, the buttons within the scrollrect will fill in from the top. You can see the tutor actually make this change in the video.