Show only the current gameobjects of an array

Hello, I need to navigate between the gameobjects of an array using next and previous buttons, but only showing the selected gameobjects and hiding the rest. Someone could help me, thanks.

You're a saint! Thank you so much!

1 Answer

1

Here a script that should work :

public GameObject[] array ;
private selectedIndex = 0;

private void Start()
{
    for( int index = 0 ; index < array.Length ; ++index )
        array[index].SetActive(false);
    SelectAt( 0 ) ;
}

public void SelectNext()
{
    SelectAt( (selectedIndex + 1) % array.Length  ) ;
}

public void SelectPrevious()
{
    SelectAt( (selectedIndex == 0) ? array.Length - 1 : selectedIndex - 1  ) ;
}

public SelectAt( int index )
{
    array[selectedIndex].SetActive( false );
    selectedIndex = index ;
    array[selectedIndex].SetActive( true );
}

Try to make some effort next time, it’s not that complicated :wink:

Thanks friend!