Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

How to set the scroll position of a scroll rect transform content during runtime?

Discussion in 'UGUI & TextMesh Pro' started by GiantGrey, Aug 21, 2014.

  1. GiantGrey

    GiantGrey

    Joined:
    Jul 2, 2012
    Posts:
    266
    just playing aroung with the new GUI and just can't figure out how to simply set the scroll position of the content rect transform of a scroll rect.

    Here's my setup:
    I have a Rect transform with a scroll rect, the content of the scroll rect is a child of it with a rect transform on it.
    Now during runtime i just want to set the Position X to a specific value of the content rect transform.

    Code (CSharp):
    1. public RectTransform rt;
    2.  
    3. void Start () {
    4.     rt1.transform.position = new Vector3(300, 0, 0);
    5. }
    this just doesn't work, the panel just gets crazy and jumps to position: -1651, 351, -539
    that really doesn't make any sense to me... am i missing something?
    thanks!
     
  2. Petersfun

    Petersfun

    Joined:
    Sep 25, 2013
    Posts:
    4
    If im understanding you right, you want to transform the object itself not its component

    so..

    Code (CSharp):
    1. void Start()
    2. {
    3. transform.position = new Vector3(XValue,YValue,0);
    4. }
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    you want to set the anchoredPosition of the rect transform.
     
  4. GiantGrey

    GiantGrey

    Joined:
    Jul 2, 2012
    Posts:
    266
    thank you Tim C.
    its working with anchoredPosition