How to set multiple objects active at once?

Currently my script will only set one object from an array active at a time, trying to get it to set two instead of one. Am I using the coroutine method wrong?

Here’s my script.

// Behaviour messages
void Start()
{
m_gameJustStart = true;

    GetObstacles();
    StartCoroutine(SpawObstacles());
}

private void GetObstacles()
{
    obstacleList = new GameObject[obstaclesObj.transform.childCount];

    for (int i = obstaclesObj.transform.childCount - 1; i >= 0; i--)
    {
        obstacleList _= obstaclesObj.GetComponentsInChildren<ObstacleScript>(true)*.gameObject;*_

}
obstacleList2 = new GameObject[obstaclesObj.transform.childCount];
for (int i = obstaclesObj.transform.childCount - 1; i >= 0; i–)
{
obstacleList2 = obstaclesObj.GetComponentsInChildren(true).gameObject;
}
}
private IEnumerator SpawObstacles()
{
while (true)
{
if (!PlayerController._Instance.IsDie)
{
if (!ObstaclesIsActive && !Water_1_IsActive && !Water_2_IsActive && !Water_3_IsActive)
{
if (Random.value <= 0.2f)
{
int randomIndex = 0;
do
{randomIndex = Mathf.RoundToInt(Random.Range(0.0f, 32.0f));
} while (obstacleList[randomIndex].activeInHierarchy && obstacleList2[randomIndex].activeInHierarchy);
obstacleList[randomIndex].SetActive(true);
obstacleList2[randomIndex].SetActive(true);
ObstaclesIsActive = true;
}
else
{
CanActiveWater = true;
}
}
}
yield return new WaitForSeconds(0f);
}
}

It looks to me like obstacleList and obstacleList2 are being filled with the same references, from ObstaclesObj. This would mean that further down, these lines:

                     obstacleList[randomIndex].SetActive(true);
                     obstacleList2[randomIndex].SetActive(true);

is just setting the same object active, twice.

Thanks! that was the problem :slight_smile: