Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change position UI image in Canvas

Discussion in 'UGUI & TextMesh Pro' started by Kandrbol, Nov 21, 2018.

  1. Kandrbol

    Kandrbol

    Joined:
    Aug 15, 2018
    Posts:
    117
    I have Image in Canvas. I want change position from script. How? Still is something bad.
    Mirek
     
    muddss likes this.
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,764
  3. Kandrbol

    Kandrbol

    Joined:
    Aug 15, 2018
    Posts:
    117
    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
     
    RobbySok likes this.
  4. glock18_unity

    glock18_unity

    Joined:
    Oct 19, 2019
    Posts:
    2
    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.
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. RectTransform picture = GetComponent<RectTransform>();
    2. picture.anchoredPosition = new Vector2(picture.anchoredPosition.x, picture.anchoredPosition.y+20);
     
    Last edited: Oct 11, 2021
    Balakay612, ow3n, fantom88 and 8 others like this.
  5. Dreamaster1111

    Dreamaster1111

    Joined:
    Nov 7, 2021
    Posts:
    3
    Hi,

    It does not seem to work for Vector3. How do I move my image on Z axis?
    Thank you in advance!
     
  6. glock18_unity

    glock18_unity

    Joined:
    Oct 19, 2019
    Posts:
    2
    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.
     
  7. Wackedout

    Wackedout

    Joined:
    Aug 2, 2018
    Posts:
    14
    Just to add to this...

    If you use
    Code (CSharp):
    1. RectTransform picture = GetComponent<RectTransform>();
    2. 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.

    Code (CSharp):
    1. RectTransform picture = GetComponent<RectTransform>();
    2. picture.anchoredPosition = new Vector2(picture.anchoredPosition.x, basePositionY+20);
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,288
    Moving this post to the UI forum.
     
  9. sarthak1105

    sarthak1105

    Joined:
    Sep 25, 2022
    Posts:
    1
    this worked for me.. as i wanted to change only x posirtion
    thank you very much..