0
\$\begingroup\$

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()
{
    menuManager = GetComponentInParent<MainMenuManager>();
    levelManager = GetComponentInParent<LevelManager>();

    chapterArray = Resources.LoadAll<GameObject>("Chapters").OrderBy(go => go.name).ToArray();

    txtStatus.text = "";
    if (chapterArray == null)
    {
        txtStatus.text = "Could not find Chapters";
        return;
    }
    else
    {
        txtStatus.text = "Loading...";
        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.

enter image description here

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – realUser404 Jan 31 '17 at 11:09
  • \$\begingroup\$ how are the chapters named in the resource folder? \$\endgroup\$ – Uri Popov Jan 31 '17 at 11:15
  • \$\begingroup\$ @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" \$\endgroup\$ – Hulio VanQuest Jan 31 '17 at 11:27
  • \$\begingroup\$ You mean that you checked that chapterArray is sorted correctly, but the FillChapterList() method is placing them in the wrong order? \$\endgroup\$ – realUser404 Jan 31 '17 at 13:17
  • \$\begingroup\$ @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? \$\endgroup\$ – Hulio VanQuest Jan 31 '17 at 15:58
3
\$\begingroup\$
 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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Hulio VanQuest Feb 2 '17 at 20:03
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ That's what my answer said; maybe not so clearly. :) \$\endgroup\$ – Nicki Feb 7 '17 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.