Keep child position in place (Parent moving)

Hi,

I am trying to keep the position of a child element fixed whilst moving the parent.

I have tried the following

public GameObject og;
public RectTransform rt;

// Start is called before the first frame update
void Start()
{
rt = og.GetComponent();
}

// Update is called once per frame
void Update()
{
og.GetComponent().sizeDelta = rt.sizeDelta;
og.GetComponent().anchoredPosition = rt.anchoredPosition;
Debug.Log(og.GetComponent().anchoredPosition);
}

the rect tranform component position remains the same but the child image is still moving. Any ideas on how to achieve this?

Thanks!

Without knowing your problem it is hard to give an exact answer, but maybe this will help. Start with the child’s Position style which is either Relative, which is based on the layout relative to the parent and other children, or absolute which skips the layout and is just positioned relative to the parent. You probably want Absolute positioning for one or both elements and use the style left, right, top, bottom values. Finally, you should consider not having the child be a child of the parent, but rather a different parent for both and have them be siblings and then position from there.