Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vozochris_1 · Nov 15, 2011 at 04:51 PM · c#explosionbomb

How to Chain of Explosion?

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;

void Explode(){
Instantiate(BombExplosion,BlockBombPos,BombExplosion.transform.rotation);}

void OnCollisionEnter(Collision col){
Collider[] DestroyedObjects = Physics.OverlapSphere(this.transform.position,1.5f);
foreach(Collider BlockBomb in DestroyedObjects){

if(BlockBomb.tag == "BlockBomb"){
BlockBombPos = new Vector3(BlockBomb.transform.position.x,BlockBomb.transform.position.y,BlockBomb.transform.position.z)
SendMessage("Explode",SendMessageOptions.DontRequireReceiver);
Destroy(BlockBomb.gameObject);}}}}

BombExplosion -> Prefab, of my explosion
range -> 1.5f

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sacredgeometry · Nov 16, 2011 at 04:19 AM

http://answers.unity3d.com/questions/160959/quotchain-of-explosionsquot-destroying-gameobjects.html

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);
             }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Nov 16, 2011 at 07:17 AM 0
Share

Urgh I cant seem to get the code block to work right...regardless here it is

p.s. I removed the bits that wouldnt be relevant and added comments so you can see what everything does.

p.p.s sorry its not in java script I work in c# I might be able to translate it if its what you need.

avatar image vozochris_1 · Nov 16, 2011 at 02:33 PM 0
Share

lol, I'm C# too, thanks for answer I'll check if it works , if it works I'll mark answer as right.

avatar image vozochris_1 · Nov 16, 2011 at 03:26 PM 1
Share

Thanks, $$anonymous$$ 2, this really helped me.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

chain of explosions - Destroying gameObjects in range 2 Answers

blow up bomb when it collides with fire 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with AddExplosionForce / Exploding 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges