The code works, but its really ugly. Any insight into arraying it and making it prettier would be g

Hi, is it possible to create this as an array with a for loop? To make one plane_ at a time true. I tried implementing one person’s feedback on a post on this problem, but I kept having an error when trying to set the whole array false or true.

It actually works how I’ve done it here, but I realize it is really bad and hacky as a piece of code. And I wish it was better.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SandBlastScript : MonoBehaviour {

    public float _time;
    public float _2time;
    public float _Twotime;
    public GameObject plane_1;
    public GameObject plane_2;
    public GameObject plane_3;
    public GameObject plane_4;
    public GameObject plane_5;
    public GameObject plane_6;
    public GameObject plane_7;
    public GameObject plane_8;
    public GameObject plane_9;
    public GameObject plane_10;
    public GameObject plane_11;
    public GameObject plane_12;
    public GameObject plane_13;
    public GameObject plane_14;
    public GameObject plane_15;
    public GameObject plane_16;
    public GameObject plane_17;
    public GameObject plane_18;
    public GameObject plane_19;
    public GameObject plane_20;

    // Use this for initialization
    void Start() {
        plane_1.SetActive(false);
        plane_2.SetActive(false);
        plane_3.SetActive(false);
        plane_4.SetActive(false);
        plane_5.SetActive(false);
        plane_6.SetActive(false);
        plane_7.SetActive(false);
        plane_8.SetActive(false);
        plane_9.SetActive(false);
        plane_10.SetActive(false);
        plane_11.SetActive(false);
        plane_12.SetActive(false);
        plane_13.SetActive(false);
        plane_14.SetActive(false);
        plane_15.SetActive(false);
        plane_16.SetActive(false);
        plane_17.SetActive(false);
        plane_18.SetActive(false);
        plane_19.SetActive(false);
        plane_20.SetActive(false);
     
        StartCoroutine("NewSand");

    }

    IEnumerator NewSand()
    {
        yield return new WaitForSeconds(_Twotime);
        plane_1.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_1.SetActive(false);
        plane_2.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_2.SetActive(false);
        plane_3.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_3.SetActive(false);
        plane_4.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_4.SetActive(false);
        plane_5.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_5.SetActive(false);
        plane_6.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_6.SetActive(false);
        plane_7.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_7.SetActive(false);
        plane_8.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_8.SetActive(false);
        plane_9.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_9.SetActive(false);
        plane_10.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_10.SetActive(false);
        plane_11.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_11.SetActive(false);
        plane_12.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_12.SetActive(false);
        plane_13.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_13.SetActive(false);
        plane_14.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_14.SetActive(false);
        plane_15.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_15.SetActive(false);
        plane_16.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_16.SetActive(false);
        plane_17.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_17.SetActive(false);
        plane_18.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_18.SetActive(false);
        plane_19.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_19.SetActive(false);
        plane_20.SetActive(true);
        yield return new WaitForSeconds(_time);
        plane_1.SetActive(false);
        plane_2.SetActive(false);
        plane_3.SetActive(false);
        plane_4.SetActive(false);
        plane_5.SetActive(false);
        plane_6.SetActive(false);
        plane_7.SetActive(false);
        plane_8.SetActive(false);
        plane_9.SetActive(false);
        plane_10.SetActive(false);
        plane_11.SetActive(false);
        plane_12.SetActive(false);
        plane_13.SetActive(false);
        plane_14.SetActive(false);
        plane_15.SetActive(false);
        plane_16.SetActive(false);
        plane_17.SetActive(false);
        plane_18.SetActive(false);
        plane_19.SetActive(false);
        plane_20.SetActive(false);  

    }
}

first assemble everything in an array.
[SerializeField] GameObject[ ] gameObjects = new GameObject[20]; //You will see this in the inspector and can populate it there.

void Start(){
foreach(GameObject g in gameObjects){
g.SetActive(false);
}
StartCoroutine(“NewSand”);
}

IEnumerator NewSand(){
yield return new WaitForSeconds(_Twotime);
for(int x = 0;x<gameObjects.length;x++){
gameObjects[×].SetActive(true);
yield return new WaitForSeconds(_time);
gameObjects[×].SetActive(false);
}
foreach(GameObject g in gameObjects){
g.SetActive(false);
}
}

1 Like

Thanks so much for your help. I really appreciate it. I can’t really express how much. Peace.

2 Likes