Search Unity

Unity 4.6 UI Script : Instanciate Gameobject under scrollview and position them?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Thormor, Oct 19, 2014.

  1. Thormor

    Thormor

    Joined:
    Nov 22, 2012
    Posts:
    39
    Hello!

    I'm trying to position instanciated gameobject (through scripts) under the content of a scrollview... The problem I have is they don't show at all at the right place, so I'm confused to what to do ..


    Code (CSharp):
    1. GameObject go = Instantiate(m_scrollableObjectTemplate.gameObject) as GameObject;
    2. go.transform.parent = gameObject.transform; //Set the "Content" GO as the parent of this new object
    So I thought if I did set the local position of the object (with gaps in between) that it would work, but it doesn't:
    Code (CSharp):
    1. Vector3 newPosition = new Vector3();
    2. newPosition.y += (m_objectGap * i);
    3.  
    4. go.GetComponent<RectTransform>().localPosition = newPosition;

    Can anyone help?
     
  2. Thormor

    Thormor

    Joined:
    Nov 22, 2012
    Posts:
    39
    Alright here is the answer for others who might have the same issue as me.
    The important part is to set the position on the anchoredPosition as this is where the object really is.


    Code (CSharp):
    1. GameObject go = Instantiate(m_scrollableObjectTemplate.gameObject) as GameObject;
    2.  
    3. go.transform.parent = gameObject.transform; //Set the "Content" GO as the parent of this new object
    4.  
    5. go.GetComponent<RectTransform>().anchoredPosition = go.GetComponent<RectTransform>().position;