• 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
Question by sacredgeometry · Aug 28, 2011 at 01:12 PM · c#explosionbomb

chain of explosions - Destroying gameObjects in range

Hi there, I am having a little trouble with my code, I think its just due to the way I implemented my bomb system but when I try to create a chain of explosions.

The way I implemented it originally, was to get the destroyObjects method to call itself so that it would set of a chain reaction, I know know that this is definitely not a good way as unity just crashes out. I am guessing its causing an infinite loop or just feed-backing.

I deduced that if I am able to have "live" list management so that the list is populated and de-populated as objects go in and out of the range then I could use that but personally i am a little lost as how i would do that. Any and all suggestions are welcome.

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

 //---------------------------------------------------------------------------------------
 // NOTES & TO-DOs
 //---------------------------------------------------------------------------------------
 //
 // Bombs do explode other bombs but currently only in the scope of the origional bomb
 // i need to feed the bomb scope method to the currently exploding one so it searches for
 // all of the bombs is scope
 //---------------------------------------------------------------------------------------
 
 public class Bomb : GameItem
 {
     //static protected int bombBag = 100;
     
     // Bomblists ~ Stores all the Bombs
     public static List<GameObject> bombList = new List<GameObject>();
     
     // Movement & Force Vars
     const int noForce = 0;
     public int maxForce = 100;
     private int throwForce = 15;
     
     // Timer Vars
     private float bornTime;
     private int bombTime = 5;
     
     
     //-------------------------------------------------------------------------------------------------------------------------
     // Blast Radius and Collider Methods
     //-------------------------------------------------------------------------------------------------------------------------
     
     protected Collider [] radiusCollider;
     
     public static float radius = 3.0f;
     private SphereCollider radiusPreview;
     
     // Initialises the Radius Collider and links the collider to the 
     void INITRadius () {
         radiusCollider = Physics.OverlapSphere(transform.position, radius);
         radiusPreview = GetComponent("SphereCollider") as SphereCollider;
         radiusPreview.radius = radius;
     }
     
     public List<GameObject> inRadius () {
         
         List<GameObject> inRadiusList = new List<GameObject>();
         
         foreach (var collider in radiusCollider) {
             inRadiusList.Add((GameObject)collider.gameObject);
         }
         
         return inRadiusList;
     }
     //-------------------------------------------------------------------------------------------------------------------------
     // Bomb Detonation and Destruction Methods
     //-------------------------------------------------------------------------------------------------------------------------
     
     void bombTimer(){
         if((Time.time - bornTime) >= bombTime){
             INITRadius();
             DestroyObjects();
         }
     }
     
     void DestroyObjects () {
         foreach (var listItem in inRadius()) {
             if (listItem.tag != "NonDestructable" && listItem.tag != "Bomb") {
                 Cube.destroyBlockWChk(listItem);
                 Destroy(this.gameObject);
             }
             else if (listItem.tag != "NonDestructable" && listItem.tag == "Bomb"){
                 expolde(listItem);
                 Destroy(this.gameObject);
                 
                 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 // PUT EXPLODE OTHER BOMB METHOD HERE!
                 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 
             }
             
         }
     }
     
     void expolde(GameObject target){
         Destroy(target.gameObject);        
     }
 
     void detonateOtherExplosives(){
         GameObject[] Bombs = GameObject.FindGameObjectsWithTag("Bomb");
         if(this.transform.position == Bombs[1].transform.position){
             Debug.Log("Booom!");
         }
     }
         
     //-------------------------------------------------------------------------------------------------------------------------
     // INIT
     //-------------------------------------------------------------------------------------------------------------------------
 
     void Start ()
     {
         // Iniialises the basclass GameItem()  ****************************** <- [ IMPORTANT FOR ALL CHILDREN OF GameItem ]
         base.INIT();
         
         //Add Bomb to the bombsArray
         bombList.Add(this.gameObject);
         
         // Grabs the time at instantiation of bombs
         bornTime = Time.time;
         
         //---------------------------------------------------------------------------------------
         // Controls Force [ ADD THIS TO ITS OWN METHOD WITH PARAMETERS ] ***********************
         //---------------------------------------------------------------------------------------
 
         
         // Adds an impulse on the first frame of creation which throws it in the direction of the Players aiming ray 
         this.rigidbody.AddForce(GameObject.FindGameObjectWithTag("AimDirection").transform.forward * throwForce, ForceMode.Impulse);
         
         // This was the old aiming system using [ up / down / left / right ]
         //this.rigidbody.AddForce(PlayerInteraction.rayDirection * throwForce, ForceMode.Impulse);
         
         // Puts new Bombs into the InstanceBin
         transform.transform.parent = GameObject.FindGameObjectWithTag("InstanceBin").transform;    
     }
     
     //-------------------------------------------------------------------------------------------------------------------------
     // UPDATE
     //-------------------------------------------------------------------------------------------------------------------------
     
     void Update ()
     {
         
         bombTimer();
         
     }    
     
     //-------------------------------------------------------------------------------------------------------------------------     
     
 }

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by SilverTabby · Aug 28, 2011 at 08:30 PM

This seems too complex for what you are trying to do. You are trying to do everything in one script that controls everything. This creates an overly complex, bloated script that is too easy to make a mistake and have it run on each object.

I would:

  • DO NOT try to get into data structures if you don't need to. It's too complex to deal with dynamic allocation for something like this.

  • DO NOT use GameObject.Destroy() on any object except this.gameObject, and only do that at the END of your explode function

  • When exploding, do the OverlapSphere like you already have, but instead of destroying each GameObject, use SendMessage to call a function on that object that causes it's explosion. This is the safest, most versatile way to do this. It is easier to add onto a SendMessage setup that it is to add onto a Destroy setup. (make sure to use SendMessageOptions.DontRequireReciever)

  • Make sure that function returns without doing anything if it has already been called (prevents recursive loop)

  • I would personally wait a few frames during the explode function - this gives the engine a chance to actually handle the Destroy calls (they do not resolve until after the update cycle) and so that the player can get a bit of feedback on what is about to happen. It should also increase frame rate because you aren't trying to do too many things in one frame and also the Destroy calls will make it so that there are fewer collides in the next frame, decreasing the overhead of the next OverlapSphere.

Comment
sacredgeometry
Illogical-Ironic
jatmdm

People who like this

3 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 · Aug 30, 2011 at 05:00 PM 0
Share

That really makes a lot of sense, why do you think this is too complicated though, this is a relatively simple script compared to a lot of my others the reason i have a lot in here is because its going to be inherited from and all of the children need these same behaviours.

Thank you for the solution though I will implement that right now, I cant believe how simple that is and how i couldn't think about it in that way. Much to learn indeed.:)

avatar image SilverTabby · Aug 30, 2011 at 08:43 PM 1
Share

"Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. "

-Albert Einstein

Remember: less is more.

avatar image sacredgeometry · Aug 30, 2011 at 10:22 PM 0
Share

Haha it feels simple to me :)

avatar image

Answer by Arshia001 · Aug 28, 2011 at 04:12 PM

Comment
zmar0519

People who like this

-1 Show 2 · 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 · Aug 28, 2011 at 04:34 PM 2
Share

Helpful, thanks :P

avatar image Arshia001 · Aug 31, 2011 at 05:26 PM 1
Share

Well, I DID type two full paragraphs on how to do it, but by some miracle the text never made it! Believe me, this is NOT my idea of a joke! In a nutshell, you should mark the objects for destruction rather than actually destroy them, and only attempt to find the neighbouring mines IF this one is not marked for destruction. Look at this link, it'll give you a better idea on how to do this:

http://en.wikipedia.org/wiki/Breadth-first_search

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Chain of Explosion? 1 Answer

blow up bomb when it collides with fire 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OverlapSphere not causing damage? - Solved 2 Answers


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