Search Unity

Question UI elements moving for seemingly no reason

Discussion in 'UGUI & TextMesh Pro' started by DoubleDigitDev, May 2, 2023.

  1. DoubleDigitDev

    DoubleDigitDev

    Joined:
    Jan 17, 2022
    Posts:
    28
    I made this hacking UI for a mechanic in my game and one of the features is that panel labeled 'stabilize'.
    This panel moves around SOMETIMES in-game and I cannot figure out why. I have been keeping it in place with an animation but I ran into a roadblock which eliminated the option to keep using that workaround. The image below is how it is supposed to appear.
    upload_2023-5-2_15-36-38.png

    The image below is how it is when it moves in-game.
    upload_2023-5-2_15-52-6.png

    I have no idea why it's doing this. I have checked that it is anchored correctly, and as I said I tried animating it to stay in place. Any suggestions are greatly appreciated!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You should not need to animate it to hold it in place. In fact, depending on what property you animate, that might even be making it move!

    UI should just stay where it is, barring something else moving it.

    Common things that move stuff in a UI:

    - layouts (GridLayout Group, HorizontalLayout, VerticalLayout, etc)
    - anchoring / offsetting (or rather, mis-anchoring)
    - animations
    - code

    For code, if you have a
    someOtherThingTransform.
    reference that you intend to use to move something else, and that script happens to be ON a UI item, but you inadvertently use
    transform.
    instead, then you might get weird things happening.

    Finally, here's some reading on UI Anchoring, Scaling, CanvasScaler, etc:

    https://forum.unity.com/threads/inc...size-between-two-people.1130146/#post-7261747

    https://forum.unity.com/threads/game-ui-button-size-problem.1142650/#post-7337383
     
  3. DoubleDigitDev

    DoubleDigitDev

    Joined:
    Jan 17, 2022
    Posts:
    28
    Thank you for your reply!! I still have no idea why it is moving. I know it has something to do with the rigidbody connected to it. The mechanic relies on the rigidbody though so I cannot take it off. I Tried messing around with some settings but the only way that seems to be working is by animating it to return it to its x position. I did figure out a way that animating it works without conflicting with the mechanic, so I will keep using that workaround unless I find the root of the problem or discover a more efficient way to solve it.