Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to move recttransform by script?

Discussion in 'UGUI & TextMesh Pro' started by leegod, Oct 18, 2016.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,488
    I have panel UI, that is title object.

    When I work at unity, I want it not hinder camera, so I made its position as (-2000, -2000, 0).

    But when game start, it should become center of game screen, but how move it by script?

    I tried like
    titlePanel.transform.localPosition = new Vector3(0, 0, 0);

    but nothing happen. I don't know why unity doesn't allow it. Very upset.
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    If the ui is annoying you in the scene view, you can hide and lock the content of the ui layer.
    To do so open the layers drop down in the top right of the unity main windows and select the hide / lock icon.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @leegod

    Hi, just use rectTransform instead of typical transform, as it replaces Transform component in UI GameObjects - and it's meant to be used when working with canvas system:

    Code (csharp):
    1.  
    2. void Start ()
    3. {
    4.     this.GetComponent<RectTransform>().localPosition = Vector3.zero;
    5. }
    6.  
     
    Kayckbr likes this.
  4. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,488
    This does not work...
     
  5. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,488
    Sorry, it works. I had problem at other script's code before it reaches above. Now it works.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,192
    transform.localPosition works on ui components as well. But it appears OP had another bug in their code, so they got that fixed. (And considering RectTransform inherits from Transform anyways).

    Normally I would only use RectTransform if I needed the additional things that come with it.
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Brathnann

    Yes you are correct, I was thinking about saying something about that, but left it out.

    Also, I didn't say it didn't work and neither did I see his code to be able to know if he had problem elsewhere.

    You can use inherited functionality from Transform component in RT or use those features from Transform itself but if you instruct someone to use transform with UI items, then they are not going to (maybe) be aware of RT features like anchorMax, anchorMin, pivot and so on... which are not available in Transform.