Search Unity

Question Intantiating objects across multiple container pages

Discussion in 'Scripting' started by slaga, Apr 18, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    I have this script here that generates buttons based on an array and a serialized grid transform

    Code (CSharp):
    1. public void InitializeUI()
    2.         {
    3.             LevelItem[] levelItemsArray = LevelSystemManager.Instance.LevelData.levelItemArray;
    4.             for (int i = 0; i < levelItemsArray.Length; i++)
    5.             {
    6.                 if (i % n == 0) // sets it to 15
    7.                 {
    8.                     LevelButtonScript levelButton = Instantiate(levelBtnPrefab, levelBtnGridHolder);
    9.                    levelButton.SetLevelButton(levelItemsArray[i], i, i == LevelSystemManager.Instance.LevelData.lastUnlockedLevel);
    10.                     var page = GameObject.FindObjectOfType<UI.Pagination.PagedRect>();
    11.                     page.AddPageUsingTemplate();
    12.                 }
    13.             }
    14.         }
    Now what I'm trying to do is make that array instantiate a new page (calling the page.AddPageUsingTemplate() function which is from another script) once for every 15 items of the array.

    I have 75 levels and want to instantiate the buttons on a new game object which has a grid layout group, so it would be 15 buttons on each grid with 5 pages/grids in total.

    I do indeed get 5 pages when I run this code, but since the transform is only set to the first one the buttons don't get placed on the other pages.

    How can I place buttons after the first 15 on the following pages?
     
    Last edited: Apr 18, 2021