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).
- CardPanel Example - which strives to get its anchoredPosition to 0, so it is in the exact same position as the TargetPosition.
-
-
-