My array loads in the wrong order

I have a load all chapters script which loads up all the Chapter items in an array and instantiates the buttons in the Viewport so you can select which chapter you would like to play. The problem is the chapters load up in the wrong order, they load up as Chapter 4, Chapter 0, Chapter 1, Chapter 2, Chapter 3. Below is the code I am using.

void Start()
{
levelManager = GetComponentInParent<LevelManager>();

txtStatus.text = "";
if (chapterArray == null)
{
txtStatus.text = "Could not find Chapters";
return;
}
else
{
FillChapterList();
}
txtStatus.enabled = false;
}
void FillChapterList()
{
foreach (GameObject chapter in chapterArray)
{
GameObject listChapter = Instantiate(chapterListItemPrefab);
listChapter.transform.SetParent(chapterListParent, false);

ChapterListItem _chapterListItem = chapterListItemPrefab.GetComponent<ChapterListItem>();
chapDetails = chapter.GetComponentInChildren<ChapterDetails>();

_chapterListItem.Setup(chapDetails.chapterNumber,chapDetails.chapterName);
}
}


This is not only happening with the Chapter Select, it has been happening with all the arrays I load up in this fashion. Below is the visual side of the problem where you can see it loads the last object First.

I have been struggling with this for a while and I think its stopping me from progressing forward. Thank you for reading this and taking the time out of your day to help.

• I do not know what your specific problem is, but note that you will have a problem if you have more than 10 chapters as OrderBy(go => go.name) will put CHAPTER_10 before CHAPTER_2 – realUser404 Jan 31 '17 at 11:09
• how are the chapters named in the resource folder? – Uri Popov Jan 31 '17 at 11:15
• @realUser404 If I remove the OrderBy(go => go.name) there is no difference. It still happens. Whats happening exactly is the array populates it loads up all the chapter objects in the correct order but when I instantiate it under the viewport It sets the last one first. @UriPopov the chapters are named "Chapter_00" and I have these going up to "Chapter_04" – Hulio VanQuest Jan 31 '17 at 11:27
• You mean that you checked that chapterArray is sorted correctly, but the FillChapterList() method is placing them in the wrong order? – realUser404 Jan 31 '17 at 13:17
• @RealUser404 chapterArray is in the correct order, so the problem has to be with the FillChapterList() method. Thank you for your help, never looked at it this way before. This then begs the question, why is the FillChapterList() loading them incorrectly? – Hulio VanQuest Jan 31 '17 at 15:58

 ChapterListItem _chapterListItem = chapterListItemPrefab.GetComponent<ChapterListItem>();

You are getting your ChapterListItem from the prefab, rather than the instantiated object listChapter. This means that you're calling the setup on your prefab repeatedly.
• I need to call the ChapterListItem from the prefab so that I can setup the text for the different names of the chapters and some general info to display on the button. But even if I call this does it change the order in which the buttons are loaded? Thank you for your help, this is just very frustrating – Hulio VanQuest Feb 2 '17 at 20:03
Thanks to the awesome people on the GameDevLeague Discord channel, it got solved. Sitting back now its quite funny how easy it was to miss. I needed to change chapterListItemPrefab.GetComponent<ChapterListItem>(); to this listChapter.GetComponent<ChapterListItem>(); now it works 100%. Thank you @KingZee and @Darkon