Scrollbar shows as active, even though it's not

I’ve got a Scroll View that is filled with buttons. Both scrollbars are set to “Auto Hide and Expand Viewport”, which works: Both scrollbars are disabled if there aren’t enough buttons to fill the Scroll View.

I’m now trying to get the status of the vertical scrollbar in code:

public void Start()  {
    Scrollbar vert = scrollrect.GetComponent<ScrollRect>().verticalScrollbar;
    //I get "scrollrect" (as a GameObject) through the Inspector.
    //This script is attached to the Content.
}

But no matter what I do, the scrollbar always says that it’s active, even though it’s clearly disabled in the Hierarchy/Inspector (it’s got the slightly darker grey and it’s not ticked) while the app’s running. I’ve tried:

  • vert.IsActive()
  • vert.isActiveAndEnabled
  • vert.enabled
  • vert.gameObject.active
  • vert.gameObject.activeInHierarchy
  • vert.gameObject.activeSelf

All of these return “true”.
How do I get the actual status, so “false”?

Unity: 2017.3.1f1

hmm… i would have guessed that vert.gameObject.activeSelf would return the right value.
What you probably can do instead is this:

bool isScrollbarVisible = scrollrect.content.rect.height > scrollrect.viewport.rect.height;

@Hosnkobf
Thanks for the suggestion, I tried

bool isScrollbarVisible = scrollrect.GetComponent<ScrollRect>().content.rect.height > scrollrect.GetComponent<ScrollRect>().viewport.rect.height;
//"scrollrect" is actually the ScrollView in the inspector because I can't get direct access to the ScrollRect there

but this always returns “false”, no matter if the scrollbar is visible or not.
I thought that this might be connected to the “ScrollToSelection” script I’m using (it automatically scrolls to the selected button) but even with that disabled it doesn’t work.

My menu: click
Why I need this: Automatic navigation lets me navigate from a button to the scrollbar (if visible) but not to the “Exit” button in the top right corner and if I make it start there, it won’t let me go back to the scrollbar.
That’s why I set the navigation to “Explicit” and set it accordingly at runtime:
Button <-right/left-> Scrollbar <-right/left-> Exit (if the scrollbar is visible)
Button <-right/left-> Exit (if it’s not visible)
For this I have to know if the scrollbar is visible or not.

Is there any other way to get the status of the scrollbar indirectly? Unfortunately I can’t just assume that it will be visible if there are, let’s say more than 5 buttons because my app supports different screen resolutions.

I just tried @Hosnkobf 's suggestion again but this time in a completely new Unity project: I still got the same results.
The height of both the rect and the viewport (I also tried “.size.y” instead of “.height”) were also output as “0”.

Fortunately I found a solution in this thread:[quote=“moure, post:2, topic: 611516, username:moure”]
The problem was that i tried to get the width/height in my start function. But as it turns out the layout group scirpt (as well as content size fitters) need a frame to update before you can get the correct element size values. So you can just Invoke(“GetSize”, 0.01); on your start function to delay it a bit.
[/quote]

Also important:[quote=“Suduckgames, post:6, topic: 611516, username:Suduckgames”]
Just a note. As this is valid in most of the cases. take into account that if the first frame take more time than 0.01f it won’t calculate it as it should.

I recommend to use a Courutine waiting for the end of frame…
[/quote]

With “Invoke” everything I tried before now outputs the right state.

1 Like