• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fragmental · Feb 19, 2017 at 05:48 PM · guilistinstances

How to Instantiate UI objects in reverse?

I'm not entirely sure how to ask this, or what to search for to find answers.

Basically, I'm trying to instantiate prefabs from a list, in reverse. In other words, currently, the first prefab is instantiated at the top of the panel and the last at the bottom, in descending order. I would like them to instead instantiate in ascending order, with the first at the bottom, and the last at the top. Additionally, I would like any new prefabs to bed added to the top.

I have a Generic List of names. From this list, I instantiate the prefabs.

         foreach (var todo in todoList)
         {
             GameObject newTodo = Instantiate(todoPrefab) as GameObject;
             newTodo.GetComponentInChildren<Text>().text = todo;
             newTodo.transform.SetParent(todoPanel, false);
         }
     }

And to add a new prefab I use this

     public void AddTodo()
     {
         string todoFieldText = AddTodoField.text;
         todoList.Add(todoFieldText);    
         GameObject newTodo = Instantiate(todoPrefab) as GameObject;
         newTodo.GetComponentInChildren<Text>().text = todoFieldText;
         newTodo.transform.SetParent(todoPanel, false);
         AddTodoField.text = "";
     }

Not shown is the Input Field the added prefab gets its name from.

I thought about reversing the List itself, but I decided it would probably be better to reverse the way they are instantiated or displayed. That way added a new item to the end of the List will have the desired effect.

Any help is greatly appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gameplay4all · Feb 19, 2017 at 07:47 PM

Hey there,

The equivalent to

 foreach (var todo in todoList)

is

  for(int i = 0; i < todoList.Count; i++)

where your var todo is replaced with todoList[i]

You should look up what the syntax is for. Basically it is a start point, condition to continue and an operation to happen at the end of an iteration. Now to reverse the order you use this loop:

 for(int i = todoList.Count - 1; i >= 0; i--)

So now you can use the loop above to go from the last index, todoList.Count - 1, to the first index, 0.

Good luck implementing it!

-Gameplay4all

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fragmental · Feb 20, 2017 at 04:55 AM 0
Share

That's an excellent way to display the list in reverse. Thank you for your answer. I was wondering about the best way to do this, and I think this is a better option than trying to use Array.Reverse to reverse the List itself.

Unfortunately, there is still one problem. Every time I add a new element to the list, I would need to delete all the prefab instances and then reinstantiate them. While this would work, I don't like it.

I guess a better question would have been to ask "How can I instantiate parented ui objects above previously instantiated children?"

avatar image
0

Answer by Fragmental · Feb 20, 2017 at 06:15 AM

@gameplay4all I accepted your answer, because it's technically correct, and does answer the question asked.

However, I found another solution which also fixes the problem of pushing newly instantiated children to the top. I created a new question and answered it, for the sake order and to hopefully make it easier to find in searches. This can be found here. http://answers.unity3d.com/questions/1315749/how-can-i-instantiate-parented-ui-objects-above-pr.html

In short, I can use

 newTodo.transform.SetAsFirstSibling();

or

 newTodo.transform.SetSiblingIndex(0);

to push all new instances to the top of the list. More information at the link.

For simply displaying the previously saved generic list in reverse order, I don't know which method is better. I don't know if there are any disadvantages or advantages to either, and both may be equally valid solutions. However, stepping through the array in reverse for the initial population of the the UI list feels cleaner to me so that's what I'm using for now. Then when I add a new object I use SetAsFirstSibling().

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

gui list problem 2 Answers

Static class member for offset 1 Answer

If String is in List Then... 2 Answers

Adding an item to a Generic List copies the old one 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges