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 /
  • Help Room /
avatar image
0
Question by ZanOlsena · May 24 at 07:34 AM · stackoverflow

Stack Overflow with code for exploding barrels

So for the project I'm working on I decided to create a script that allows a game object to be "explosive" and deal damage to other entities around it based on their tag. However, upon interacting with other variants of itself on other explosive objects caught in the blast radius (in my case, a stack of explosive barrels), a Stack Overflow occurs. My code is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ExplosiveObject : MonoBehaviour
 {
     public int maxHealth = 100;
     public int currentHealth;
 
     float radius = 3;
     float force = 500;
 
     public int explosionDamage = 100;
 
     [SerializeField] GameObject explosionParticle;
     // Start is called before the first frame update
     void Start()
     {
         currentHealth = maxHealth;
     }
 
     public void TakeDamage(int damage)
     {
         currentHealth -= damage;
 
         if (currentHealth <= 0)
         {
             //if (brokenObjectVarient != null)
             //{
             //    Instantiate(brokenObjectVarient, transform.position, transform.rotation);
             //}
 
             Explode();
         }
     }
     void Explode()
     {
         if (explosionParticle != null)
         {
             GameObject spawnedParticle = Instantiate(explosionParticle, transform.position, transform.rotation);
             Destroy(spawnedParticle, 1);
         }
 
         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
         foreach (Collider nearbyObject in colliders)
         {
             if (nearbyObject.tag == "Enemy")
             {
                 nearbyObject.GetComponent<Target>().TakeDamage(explosionDamage);
             }
 
             if (nearbyObject.tag == "Breakable")
             {
                 nearbyObject.GetComponent<BreakableObject>().TakeDamage(explosionDamage);
             }
 
             //Causes stackoverflow if another barrel is within the blast radius
             if (nearbyObject.tag == "Explosive")
             {
                 nearbyObject.GetComponent<ExplosiveObject>().TakeDamage(explosionDamage);
             }
 
             if (nearbyObject.tag == "Player")
             {
                 nearbyObject.GetComponent<PlayerMovement>().PlayerTakeDamage(explosionDamage / 2);
             }
 
             Rigidbody rb = GetComponent<Rigidbody>();
             if (rb != null)
             {
                 rb.AddExplosionForce(force, transform.position, radius);
             }
         }
         Destroy(gameObject);
     }
 }
 

It should be noted that on its own this code works fine, problems only occur when another object with ExplosiveObject attached is nearby.

Now, I have two theories as to why this is happening: either ExplosiveObject calling TakeDamage on another variant of itself is causing the stack overflow, or ExplosiveObject's own explosion is somehow calling its own TakeDamage function. That second one makes less sense because the code works fine on its own, but those two were the best ideas I had at what might be the cause. Any help on this is greatly appreciated, since I haven't seen anyone else doing something similar that causes a stack overflow.

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
1
Best Answer

Answer by Hellium · May 24 at 10:08 AM

You may want to check if currentHealth is not already less than 0 when TakeDamage is called, otherwise, A damages B, which damages A, which damages B, which damages B, and so on.

Destroy does not destroy the object instantly.

  public void TakeDamage(int damage)
  {
      // Already exploding
      if (currentHealth <= 0)
          return;

      currentHealth -= damage;
 
      if (currentHealth <= 0)
      {
          //if (brokenObjectVarient != null)
          //{
          //    Instantiate(brokenObjectVarient, transform.position, transform.rotation);
          //}
 
          Explode();
      }
  }


I would also check if you're not trying to call TakeDamage on yourself in Explode

  void Explode()
  {
      if (explosionParticle != null)
      {
          GameObject spawnedParticle = Instantiate(explosionParticle, transform.position, transform.rotation);
          Destroy(spawnedParticle, 1);
      }
 
      Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
      foreach (Collider nearbyObject in colliders)
      {
            if(nearbyObject.gameObject == gameObject)
                 continue;

            // ...
      }
Comment
Add comment · Show 1 · 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 ZanOlsena · May 24 at 10:54 PM 0
Share

Ah, I didn't even think of checking if currentHealth was already below zero! That fixed it, and I added in the second check for good measure. Thanks a ton!

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

184 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stack Overflow Exception Unity C# Scripting 1 Answer

StackOverflow Problem when consistently checking a gameObject 1 Answer

stackoverflow, minecraft terrain, finding neightbours 1 Answer

Random.Range throws StackOverFlow exception in recursion 1 Answer

Stack pop and push for unityscript? 0 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