I’m trying to make a book where if the user press the pages it SetActive(false) every GameObject except the selected GameObject.
public GameObject[] bookPages;
int currentPage;
public void whatPage ( )
{
int pages = 0;
while ( pages < bookPages.Length )
{
if ( pages == currentPage )
{
Debug.Log ( "CURRENT PAGE" + currentPage );
bookPages [ currentPage ].SetActive ( true );
pages++;
continue;
}
bookPages [ pages ].SetActive ( false );
Debug.Log ( pages );
pages++;
}
}
public void pageFlu ( )
{
currentPage = 1;
whatPage ( );
bookPages [ currentPage ].SetActive ( true );
}
I have tried the continue method.
>Solution :
A better approach to this problem would be keeping track of the last active page and deactivating it when any other page is being selected. That would be a lot faster.
You could also write a function that takes the selected page as parameter, deactivates current page, assigns current page to the selected page parameter and activates the selected page. Something like this:
public GameObject[] bookPages;
int currentPage;
public void DeactivateAllPages()
{
foreach (GameObject page in bookPages)
{
page.SetActive(false);
}
}
public void SelectPage(int pageIndex)
{
bookPages[currangePage].SetActive(false);
currangePage = pageIndex;
bookPages[currangePage].SetActive(true);
}