ScrollContainer, VBoxContainer with childs - scroll to child

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bingo

Hi, I have got a ScrollContainer with a VBoxContainer child node and some buttons are children of that. The scrolling works perfecly fine. But how can I scroll to a specific child of the VBoxContainer? I’m using C#.

:bust_in_silhouette: Reply From: miskotam

Assuming that you have a fix button height, you could calculate the scroll position like this:

var scrollPosition = buttonIndex * buttonHeight;

And set the ScrollVertical property on your ScrollContainer:

scrollContainer.ScrollVertical = scrollPosition;

If you have spacing between the items, you could use the GetConstant of the VBoxContainer with the separation parameter to get it:

var itemSpacing = vBoxContainer.GetConstant("separation");
var scrollPosition = buttonIndex * buttonHeight + buttonIndex * itemSpacing;