Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding elements to the top of a scrollview

Discussion in 'Scripting' started by K2mia, Feb 13, 2016.

  1. K2mia

    K2mia

    Joined:
    Jan 25, 2016
    Posts:
    12
    Hi folks, am working on a chat system for my game. I have a chatPanel prefab which is used to add new messages to my chat's scrollView. Here is the current code:

    Code (CSharp):
    1.  
    2.      GameObject panelChatMesg = Instantiate(chatPanelPrefab);
    3.       panelChatMesg.GetComponentInChildren<Text>().text = sText;
    4.       panelChatMesg.transform.SetParent( GameObject.Find("ContentChat").transform, false);
    5.  
    It works fine but new messages are appended to the end of the scrollView. I would like for new messages to appear at the top instead (and push older messages down), is there an easy way to tweak my code to accomplish this goal?

    Thank you,
    -K2
     
  2. K2mia

    K2mia

    Joined:
    Jan 25, 2016
    Posts:
    12
    I found the answer... I needed to add this line of code to make this work:

    Code (CSharp):
    1. panelChatMesg.transform.SetAsFirstSibling();
     
  3. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    280