Hello, sorry if I missed a question related to this. If there is one out there please point me to it.
I have a scrollbar behaving oddly. I wrote a script to change the scrollbar value of a dropdown list to match the currently selected object when the user expands a dropdown so that the selected item is visible. I do this by finding index of the active element(of the active children) and dividing this by the total number of active children associated with the dropdown to come up with a 0-1 number to position the scroll bar.
The following script behaves as expected when the selected value is anything besides the highest index (ex: activeChild = 12 and activeChildren = 24). However, when the current selection is the last option (ex activeChild = 24 and activeChildren = 24) the scrollbar sets it’s value to 1.
private IEnumerator SetScrollbarValue() {
int activeChildren = 0;
int activeChild = 0;
//Get all active child objects in dropdown list
for (int i = 0; i < transform.childCount - 1; i++) {
if (transform.GetChild(i).gameObject.activeInHierarchy) {
activeChildren++;
}
}
//get the index of the currently selected child
for (int i = 0; i < transform.childCount - 1; i++) {
if (transform.GetChild(i).gameObject.activeInHierarchy) {
if (!transform.GetChild(i).GetComponent<Toggle>().isOn) {
activeChild++;
} else {
break;
}
}
}
//get the scrollbar
Scrollbar scrollbar = transform.GetComponentInParent<ScrollRect>().verticalScrollbar;
//set the value of the scrollbar for BottomToTop direction
if (scrollbar.direction == Scrollbar.Direction.BottomToTop) {
float value = (1.0f - ((float)activeChild / (float)activeChildren)); //Case where activeChild = 0 and activeChildren = 0
scrollbar.value = value;
yield return new WaitForEndOfFrame();
Debug.Log("value = " + value); //logs 0
Debug.Log("Scrollbar value = " + scrollbar.value); //logs 1
}
}
Now, when I manually check for 0 and force the value to be near 0 it keeps the number and acts as expected (I guess). Note the change directly after ‘value’ is created
private IEnumerator SetScrollbarValue() {
int activeChildren = 0;
int activeChild = 0;
//Get all active child objects in dropdown list
for (int i = 0; i < transform.childCount - 1; i++) {
if (transform.GetChild(i).gameObject.activeInHierarchy) {
activeChildren++;
}
}
//get the index of the currently selected child
for (int i = 0; i < transform.childCount - 1; i++) {
if (transform.GetChild(i).gameObject.activeInHierarchy) {
if (!transform.GetChild(i).GetComponent<Toggle>().isOn) {
activeChild++;
} else {
break;
}
}
}
//get the scrollbar
Scrollbar scrollbar = transform.GetComponentInParent<ScrollRect>().verticalScrollbar;
//set the value of the scrollbar for BottomToTop direction
if (scrollbar.direction == Scrollbar.Direction.BottomToTop) {
float value = (1.0f - ((float)activeChild / (float)activeChildren)); //Case where activeChild = 0 and activeChildren = 0
//Force 0 to be near 0
if (value == 0.0f) {
value = 0.0001f;
}
scrollbar.value = value;
yield return new WaitForEndOfFrame();
Debug.Log("value = " + value); //logs 0.0001
Debug.Log("Scrollbar value = " + scrollbar.value); //logs 9.994933E-05
}
}
Any info as to why this may be happening or what I am overlooking would be great. I know this script is severely limited at the moment. But I would like to expand on it to make a fun little utility to help with my use of dropdown menus in Unity.
Thank you,