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.
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;
// ...
}
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
![](https://koobas.hobune.stream/wayback/20220613060928im_/https://answers.unity.com/themes/thub/images/avi.jpg)