how to swap image position

hi,
I am learning UI with canvas and images.
I know how to translate image : name.GetComponent(RectTransform).anchoredPosition = new Vector2(x,y);

Now I would like to swap position of 2 images. I tried this :

name.GetComponent(RectTransform).anchoredPosition = name2.GetComponent(RectTransform).anchoredPosition;

but it doesnt work (which is obvious for experts). Do anyone know how to swap images position with a simple method ? or just how to move a image to another one’s position. Thx.

3 Answers

3

use this instead

name.GetComponent<RectTransform>().AnchoredPosition = new Vector2(name2.GetComponent<RectTransform>().AnchoredPosition.x, name2.GetComponent<RectTransform>().AnchoredPosition.y);

Anchored Position is a Vector and you can’t just assign a value of a Vector like assigning int value

Vector2 l_temp = name.GetComponent(RectTransform).anchoredPosition;
name.GetComponent(RectTransform).anchoredPosition = name2.GetComponent(RectTransform).anchoredPosition;
name2.GetComponent(RectTransform).anchoredPosition = l_temp;

Thanks for your answers.

I knew the trick to swap basic gameobject position with 1_temp.
I tried the answers but the image dont move because the 2 images seems to have the same anchoredposition (both surrounded by their own rect transform).

I found the solution : they have to have the same “rect transform” to make it work, or/and the same “canvas” I dont know. But in my case the responsive dont work fine anymore. So hard to deal with those “images” t_t

Something like that would work, yes