GameObject Array problem.

Hi, I’m sure this code is way off base for what I’m trying to achieve, but what I’m trying to do is:

have an array of 54 GameObjects, but I want them to only be SetActive(true) one at a time, in order, and separated by a time variable _time.

Any advice or insight would be great.

public class blastArray : MonoBehaviour
{

    public GameObject[] sandArray;
    public GameObject DustBlast1;
    public float _time;

    // Use this for initialization
    void Start()
    {
        sandArray[0, 54].SetActive(false);

    }

    // Update is called once per frame
    void Update()
    {
        if (DustBlast1)
        {
            StartCoroutine("NewSand");
        }

    }

    IEnumerator NewSand()

    {
        sandArray[0].SetActive(true);
        sandArray[!= 0].SetActive(false);
        yield return new WaitForSeconds(_time);
        sandArray[1].SetActive(true);
        sandArray[!= 1].SetActive(false);
        yield return new WaitForSeconds(_time);
        //.....

    }
}

Well, you would use a for loop in that. You haven’t initialized your array in your start function and you can’t set them all active like that, you have to loop through them:

sandArray = new GameObject[54];
for(int x=0;x<sandArray.length;x++){
sandArray[x].SetActive(false);

}

and in newSand function:

for(int x=0;x<sandArray.length;x++){
sandArray[x].setActive(true);
yield return new WaitForSeconds(_time);

}

Loops are what you’re looking for.
Also, if ‘SandBlast1’ is assigned you’ll start a new coroutine every frame. You need to handle the situation that another blast is started. Either reset the coroutine or start a new one, or prevent another one from starting in the first place.

The array is most-likely populated via the inspector, you don’t wanna create it yourself programmatiaclly. :stuck_out_tongue:

Thanks so much your help and the education it offers.