Hello, can you please give me an example of chain of explosions? I tried with OverlapSphere’s but no luck…maybe it’s with OverlapSphere but I can’t do it… it Creates and Destroys maximum of 2 bombs. How can I make it Explode and Destroy all objects in chain? Something like create OverlapSphere for every Destroyed object or before Destroying. And if there is another object with tag BlockBomb within range of Destroyed/Destroying object. Explode and Destroy object, and so on, or any other way. Please help, here is my code for Bomb:
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
public GameObject BombExplosion;
private Vector3 BlockBombPos;
I asked this question recently and the solution that worked best was Using SendMessage to call a explosion function on the object.
A run down is, so say you have a bomb prefab, each has an overlap sphere which populates an array with all the colliders inside the spheres radius. What you want to do is attach a script to the bomb and make a destroy function that when called iterates through that array and sends the destroy function on those objects.
That should do the trick.
p.s. as SilverTabby pointed out its probably a good idea to leave a few frames between each destroy call so maybe add a few yields.
Here is my script
using UnityEngine;
using System.Collections;
public class BombScript : MonoBehaviour {
protected Collider[] radiusCollider;
public static float radius = 3.0f;
private float bornTime;
private int bombTime = 5;
// INIT
void Start () {
bornTime = Time.time;
}
// Update is called once per frame
void Update () {
radiusCollider = Physics.OverlapSphere(transform.position, radius);
bombMechanics();
}
void bombMechanics(){
// This if statement is basically the bombs timer and is the second value is set in the bombTime variable
if((Time.time - bornTime) >= bombTime){
//This Destroy Call destroys the bomb you dropped once the time runs out.
Destroy(this.gameObject);
// This foreach iterates through all the colliders inside the overlap sphere and sendMessages the bombExplode method
foreach (var bombInstance in radiusCollider) {
bombInstance.SendMessage("bombExplode", SendMessageOptions.DontRequireReceiver);
}
}
}
IEnumerator bombExplode(){
// after this is called it waits for a 10th of a second and then calls destroy on this object (for the sake of explanation this would be all
// the other bombs that were in the overlap sphere)
yield return new WaitForSeconds(0.1f);
Destroy(this.gameObject);
// this then checks all of the objects that was in its radius spheres before they were destroyed and sends the message to run this method //and so on... until all the bombs are destroyed
foreach (var bombInstance in radiusCollider) {
bombInstance.SendMessage("bombExplode", bombInstance, SendMessageOptions.DontRequireReceiver);
}
}
}