Search Unity

transform.localPosition incorrect afting adding and removing vertical layout group

Discussion in 'Scripting' started by thevvulf, Sep 19, 2021.

  1. thevvulf

    thevvulf

    Joined:
    Oct 31, 2020
    Posts:
    36
    I'm working on a small project, and a crucial part is placing stuff. To let the player chose what they place, I have a UI thing that slides out when you press a button and shows a bunch of the objects they can chose. To let them cycle through objects, there's a little arrow on the bottom that moves each object down a bit and basically wraps it back to the top once its off the screen. Code (will optimize once working):
    Code (CSharp):
    1. foreach (Transform item in transform)
    2.         {
    3.             if(!Mathf.Approximately(item.localPosition.y, -100) && !Mathf.Approximately(item.localPosition.y, -250)){
    4.                 LeanTween.moveLocalY(item.gameObject, item.localPosition.y - 100, timeToMove);
    5.             }
    6.             if(Mathf.Approximately(item.localPosition.y, -100)){
    7.                 LeanTween.moveLocalY(item.gameObject, item.localPosition.y - 150, timeToMove);
    8.             }
    9.             if(Mathf.Approximately(item.localPosition.y, -250)){
    10.                 Transform highestChild = transform.GetChild(0);
    11.                 item.localPosition = new Vector3(item.localPosition.x, highestChild.localPosition.y + 100, 0);
    12.                 LeanTween.moveLocalY(item.gameObject, item.localPosition.y - 100, timeToMove / 2);
    13.                 item.SetAsFirstSibling();
    14.             }
    15.         }
    I had it working fine, so I went off to working on adding objects, since they can unlock them. Obviously once they unlock something, I want it to be in the list, so I needed a way of adding it to the list in a certain way. To do this, I thought of possibly messing with a layout group to set them up properly just once, then I can modify it after. This didn't work, so I got rid of the component. Then the list movement stopped working. The rect transform on the objects was suddenly showing a different value. It started showing the positions relative to the top left of the parent object, not the middle like it was before. For example, the object in the middle of the background UI used to just be at 0, 0, 0, but now its at 34, -209, 0. I used debug.logs to show the objects actual positions and it looked perfectly fine. Both transform.localPosition and position looked exactly as they had before, but they stopped working even though nothing changed. I've looked up countless sources but none had quite the same issue, so I'm completely lost.