[SOLVED] How To Loop Through Array of GameObjects, Activate / Deactivate Over X Time

I have a gameobject array with an index of 50. I also have a float array called startExplosions with an index of 50. Each gameobject has an explosion prefab attached to it. My goal is to create a script that goes through each index of the array of gameobjects, activates that specific index of game object after X time (time being whatever value is assigned to the index of startExplosions), deactivates it after the explosion finishes, and continues activating / deactivating each game object index every X seconds (startExplosions) until I tell it to stop. My head keeps spinning trying to get this to loop and time properly and I end up crashing Unity from accidental endless loops on loops. :hushed:

So you have an array of game objects, and you want to go through the list 1 by 1 activiating each explosion?

Or do you want to go through all of it at once setting the timers and then activating the explosions after? (I’m assuming the 1st one is what your going for)

Cause It looks like your trying to have two arrays where one is the gameobjects and the other is an array of the explosion that is attached to each gameobject (assuming is the gameobjects that you were referencing in the other array).

In that instance to save yourself from doing multiple loops, you could have a script on each gameobject that would reference that explosion prefab that is attached to that game object and also a timer value. Then in the script just have an array of those scripts so that way in the loop, if you want to activate that specific game object’s explosion prefab you can just by stepping through the loop and invoking the method name with a set interval (or you could use courtine, but I like invoking better since its easier)

I’m trying to create the effect of 50 explosions going off in a sequence. Being able to assign the explosion time to the game object would give me the ability to create different sequences of explosions. If I went through the list 1 by 1 there would be a delay to lets say, explosion # 50, if the activation time was set to 6 seconds but it had to wait for the other 49 explosions to go off first.5318547--535038--game.jpg

I currently have the 50 gameobjects as child objects to a parent object. The script is attached to the parent object. I see what you’re saying. I’ll give that a try.

Ok so you want them all to be set at once (If I understood right). In that case you can do somethin like this. A script on each gameobject.

public class Exploder : MonoBehaviour
{
    //a script that would be on each gameobject
    //public variables so the parent script can see this stuff
    public GameObject explosion;
    public float interval;

    //so the parent script will call this function and that will set off the timer here that is indepdent of all the other timers.
    public void StartExplosion()
    {
        //You could use a Coroutine here but I like Invokes since they are easier to work with
        Invoke("SpawnExplosion", interval);
    }

    public void SpawnExplosion()
    {
        //spawn the explosion prefab
        GameObject explosionInstance = Instantiate(explosion, transform.position, transform.rotation);
    }
}

And then in the parent script, you have an array of the Exploder objects.

public class ParentScript : MonoBehaviour
{
    public List<Exploder> exploders = new List<Exploder>();

    public void SetExplosionsOff()
    {
        foreach(Exploder exploder in exploders)
        {
            exploder.StartExplosion();
        }
    }
}

I created a script on each object and it worked beautifully!

public class Explode : MonoBehaviour
{
    public float timer;
    public GameObject particle;

    // Start is called before the first frame update
    void Start()
    {
        Invoke("Explosion", timer);
    }

    void Explosion()
    {
        Instantiate(particle, transform.position, transform.rotation);
    }
}

I did not need to create a parent script to acheive this effect. Each object that has this script assigned to it has a box collider with a tag set so if the ball collides with it, the level resets. Is there a way to have the collider activate with the explosion and then deactivate so if there isn’t an explosion happening, it wont trigger death for the player?

Sure it is, In the same script you can grab a Collider (I assume the collider will be already attached to the gameobject) and then activate it once the explosion happens, and if you want to deactivate it again we can do another Invoke. I added a timer so it disables after an explosion, which you probably dont need but its there to show you how you can use Invokes

    public float timer;
    public float disableColliderTimer; //interval for disabling the collider
    public GameObject particle;

    private Collider collider;

    void Start()
    {
        collider = GetComponent<Collider>(); //get the collider when the game starts

        collider.enabled = false; //disable it so we can enable it again once the explosion happens

        Invoke("Explosion", timer);
    }

    void Explosion()
    {
        Instantiate(particle, transform.position, transform.rotation);

        collider.enabled = true; //enable the collider

        Invoke("DisableCollider", disableColliderTimer);
    }

    private void DisableCollider()
    {
        collider.enabled = false;
    }

This worked on disabling the collider, it set it to true during the explosion and then the disable collider function is invoked. After that it stays disabled, even when set true again.

Yeah then its working as intended, I kinda expected you to retool it but thats fine.

I expected though after an explosion that the animation would finish then you would disable the collider since the explosion is done. But if you want it to stay enabled after the explosion you can simply get rid of the Invoke function and the disable collider function. But all of that should work as expected I’m not sure what else you want to do. I’ll re-read your thing again just to make sure

Ok I re-read your thing, my bad but you want it to continue exploding after every X seconds? in that case you can simple change the Invoke to an InvokeRepeating.

So it works like this

InvokeRepeating(“method name”, start interval, repeating interval)

so it called the invoke repeating but it wont call the method until it reaches the start interval time, then once it does it will reach the repeating interval. so it reaches that interval and then wait until it reaches again. If you want to stop the InvokeRepeating or an Invoke you just simply call CancelInvoke(“method name”)

Thank you so much for the help! A huge leap forward for me to be able to build levels with this.