Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Disabling every GameObject except one

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading