How to make an array list set to false gradually instead of instantly?

Below is the code for setting all the array in my list to false instantly. So what I would like to achieve is to set the list of array to false gradually.

(example: example I have 10 box in the scene when I click on object 2 I would like to show object 1 and 2 then the rest of the object in the array will set to false gradually)

Anyone have any ideas how I could achieve the effect?

public GameObject test;

public void ArrayObj()
{

    for (int i = 0; i < test.Length; i++)
    {
        test*.SetActive(false);*

}
test[0].SetActive(true);
test[1].SetActive(true);
}

You should use [coroutines][1] for that. When you have a coroutine you just put yield return inside the for loop and specify how long you want to wait.

Below is an example of what you want with 1 second intervals:

public IEnumerator SetInactiveGradually()
{
	 for (int i = 0; i < test.Length; i++)
     {
         test*.SetActive(false);*
  •   yield return new WaitForSeconds(1);*
    

}
}
Just remember, t ostart a coroutine you have to use:
StartCoroutine(SetInactiveGradually());
[1]: Unity - Manual: Coroutines

public void ArrayObj()
{
StartCoroutine( TurnOffGameObjects( test, new WaitForSeconds( 0.1f ), 2 ) ;
test[0].SetActive(true);
test[1].SetActive(true);
}

private IEnumerator TurnOffGameObjects( GameObject[] gameObjects, YieldInstruction wait = null, int startIndex = 0, int endIndex = -1 )
{
     // Clamp the indices to have valid values regarding the array length
     startIndex = Mathf.Clamp( startIndex, 0, gameObjects.Length ) ;

     if( endIndex < 0 ) endIndex = gameObjects.Length ;
     else endIndex = Mathf.Clamp( endIndex, startIndex, gameObjects.Length ) ;

      // Turn off the gameObjects and wait between two deactivations
     for ( int i = startIndex ; i < endIndex ; i++)
     {
         gameObjects*.SetActive(false);*

yield return wait ;
}
}