UI element's anchored position reset every update

Hi community!

Some things that should be simple with Unity takes hours, and in this case days, to solve.
I am trying to move a UI Panel by altering its anchoredPosition, but it seems the anchoredPosition is reset every frame to what it was before. I have searched the entire solution, and the only code that affects the anchoredPosition is this, in the panel’s own update function:

timer += Time.deltaTime;
if (timer > 1)
{
    Debug.Log("Current anchored position for " + gameObject.name + " = " +  thisTransform.anchoredPosition.ToString());
}
thisTransform.anchoredPosition = Vector2.MoveTowards(thisTransform.anchoredPosition, new Vector2(0,0), moveSpeed * Time.deltaTime);
if (timer > 1)
{
    timer = 0;
    Debug.Log("Current anchored position for " + gameObject.name + " = " + thisTransform.anchoredPosition.ToString());
}

So, you see I log the anchoredPosition every second, first before the move, and then after the move. It is supposed to move towards 0, 0. The output shows the following:

[12:50:15] Current anchored position for CardPanel Example = (-456.0, 345.0)
[12:50:15] Current anchored position for CardPanel Example = (-454.5, 343.9)
[12:50:16] Current anchored position for CardPanel Example = (-456.0, 345.0)
[12:50:16] Current anchored position for CardPanel Example = (-453.4, 343.0)

So, you see the panel starts at -456, 345 and actually moves slightly, to -454.5, 343.9.
But then, 1 second later, it starts again at -456, 345. Why was it reset?? This panel is not the child of any layout group, and no other code affects its anchoredPosition. Changing the values in the inspector changes its starting position correctly, but then it can never move from that position during runtime.

This is driving me crazy.
Someone please tell me what I am getting wrong!

(For additional context. The panel is the child of another panel, which is positioned using a Horizontal Layout Group on its parent. So the parent’s transform is affected by this layout group, but the panel shouldn’t be. So the hierarchy looks like this:

  • handPanel - with Horizontal Layout Group Component
      • TargetPosition - a panel which is positioned by handPanel’s Layout Group.
          • CardPanel Example - which strives to get its anchoredPosition to 0, so it is in the exact same position as the TargetPosition.
            If this error is because I am not understanding the dynamics of the layout components, please tell me how to correct this).

UI != 2D.

You are asking this in wrong forum. There is a separate uGUI & TextMesh Pro forum.

Anyway, have you tried the same with localPosition / position?

And do you have a specific reason to use anchored position?

I’m not quite sure what your Horizontal Layout Group might be doing… did you first try something similar without a layout group to see if everything else in your code works OK?

1 Like

Thank you eses, I did not know about the other forum. I will ask there instead.
Just as a quick follow-up, I’ve now tried localPosition with the exact same result.
I’ve also tried position with the exact same result. Which is strange. I wouldn’t exepect the changes to localPosition and position to have any influence on the anchoredPosition values, but the outputs are the same (still logging the anchoredPosition).

I’ve also tried removing the Horizontal Layout Group, but with no effect.

The reason I’ve been trying to use anchoredPosition is that most forums say that’s what you should use.

I have finally found the problem: animations. There was an animation that keyed the anchoredPosition. The value wasn’t even changed in the animation, it was just part of the animation properties, which meant that - since the animation was looping in the animator - the anchoredPosition was reset as part of that animation.

So if you’re experiencing the same problems, check if you have any animation clips that may be altering the anchoredPosition.