Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Vertical ScrollBar wrong initial position value if Canvas was disabled before play

Discussion in 'UGUI & TextMesh Pro' started by R1PFake, Sep 8, 2022.

  1. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    526
    I have a ScrollRect inside a Canvas, the Canvas is disabled (only the canvas component, not the whole game object!) in the scene.

    When I press play some code fills the scroll viewer content children and then sets the Canvas to enabled.

    My problem is that the initial vertical scroll bar value is wrong in this case, it's kinda "random" but mostly somewhere around the middle. It should start at the top.

    The scrolling etc. all works fine, I can drag the scroll bar up and down with the mouse and everything works, it's just that the initial position is wrong.

    This issue seems to be caused by the fact that the parent canvas component is disabled in the scene view and later enabled in the code. Because if I enable the canvas in the scene view and then start the play mode (same code runs, still appending the same children in code etc.) the intial vertical scroll bar position is correct, at the very top.

    I tried some workarounds by changing the values in code after adding the children, but that didn't work at first. The only workaround I found so far is to start a coroutine which waits one frame (after the children where added and the canvas was set to enabled) and after this frame delay I set the normalizedPosition to top by code and then it works.

    So my question is: Why is this workaround required if the canvas was disabled before play and enabled by code but not in any other case?

    I assume it's some kind of inital layout timing issue, since the workaround (scrolling to top by code) also requires one frame delay.

    Did anyone experience a similar issue? I will keep my workaround for now, but it's kinda ugly and I would like to find a better way, which doesn't require custom code.


    Btw this is kinda off topic but I noticed something weird with the "Content" child of the ScrollViewer. When the canvas component is disabled in the editor (without play mode) the transform values of the "Content" child go crazy and start to change by them self. At one point the Pos Y was over 5000 and Height over 21000. The values go back to "normal" (and stop changing) if the parent Canvas component is enabled again. Is this normal?
     
    Last edited: Sep 8, 2022
    el_Guero and Hozgen90 like this.
  2. Hozgen90

    Hozgen90

    Joined:
    Jan 20, 2021
    Posts:
    18
    i have the same weird problem and i couldnt have solved it so far. Did you find any solution about this issue?
     
    el_Guero likes this.
  3. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    Same problem here. What is the solution? At least glad I'm not alone lol
     
  4. Hozgen90

    Hozgen90

    Joined:
    Jan 20, 2021
    Posts:
    18
    You will have to position it with the code on awake ^^
     
    el_Guero likes this.