Search Unity

Unity UI Scrollbar shows as active, even though it's not

Discussion in 'UGUI & TextMesh Pro' started by SimRuJ, Jan 17, 2019.

  1. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    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:

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

    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
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    hmm.. i would have guessed that vert.gameObject.activeSelf would return the right value.
    What you probably can do instead is this:
    Code (CSharp):
    1. bool isScrollbarVisible = scrollrect.content.rect.height > scrollrect.viewport.rect.height;
     
  3. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    @Hosnkobf
    Thanks for the suggestion, I tried
    Code (CSharp):
    1. bool isScrollbarVisible = scrollrect.GetComponent<ScrollRect>().content.rect.height > scrollrect.GetComponent<ScrollRect>().viewport.rect.height;
    2. //"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.
     
    Last edited: Jan 21, 2019
  4. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    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:
    Also important:
    With "Invoke" everything I tried before now outputs the right state.
     
    Hosnkobf likes this.