I have Image in Canvas. I want change position from script. How? Still is something bad.
Mirek
Yes. I have read it. But is not there a simply demonstration of how to write it? It still does not work.
I finally used classical for gameObject.
gameObject.transform.position = new Vector3(62f, 888f, 0f)
But I would like to learn to use RectTransform.
Mirek
This is the first result that shows in Google when looking up the problem, and shamefully no actual solution, even after over 2 years.
This is how I do it. In the example I move a picture up by 20.
GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 20);
Edit: the instruction above sets the picture distance from the anchoredPosition, which means, it will move the picture up by 20, only if the picture’s current anchoredPosition is (0, 0) . If this position is anything different, then you should add to it:
RectTransform picture = GetComponent<RectTransform>();
picture.anchoredPosition = new Vector2(picture.anchoredPosition.x, picture.anchoredPosition.y+20);
Hi,
It does not seem to work for Vector3. How do I move my image on Z axis?
Thank you in advance!
As far as I know, RectTransform doesn’t use the Z axis at all, so I don’t think you can use it with Vector3.
Just to add to this…
If you use
RectTransform picture = GetComponent<RectTransform>();
picture.anchoredPosition = new Vector2(picture.anchoredPosition.x, picture.anchoredPosition.y+20);
Then you want to add a basePositionY variable to the class to hold the starting anchor point when “Start” is called.
RectTransform picture = GetComponent<RectTransform>();
picture.anchoredPosition = new Vector2(picture.anchoredPosition.x, basePositionY+20);
Moving this post to the UI forum.
this worked for me… as i wanted to change only x posirtion
thank you very much…