Why isn't my object being destroyed?
I've seen a fair few posts about these issues, and the overall moral is that I'm likely doing something wrong. That said, I don't know what. Have a look:
public void Activate()
{
//Get all the colliders we hit
Collider[] hitThings = Physics.OverlapBox(collider.bounds.center, collider.bounds.extents, transform.rotation);
//Send them right to the Damage Dealer
foreach(Collider col in hitThings)
{
//Bit confusing wording here, to clarify:
//Ask the targetted damage dealer to deal damage to the hit colliders
target.DealDamage(col);
}
//Now die die die!!! That is, clean yo self up! ... Delete this hitbox
Destroy(gameObject);
}
My ridiculous comments aside, this is called on my hitbox object directly after it is instantiated. I'm just using the collider as a visual way to set up the overlapBox, but it's that last line that is causing me so many problems. Actually, it's just causing me one problem: The object doesn't get destroyed. Note that I've tried GameObject.Destroy(this.gameObject) as well, and all combinations of the two, to no avail. I even tried DestroyImmediate, but no dice. Any help would be appreciated!
Answer by Spaceminer_ · Jan 04, 2017 at 05:56 PM
So, turns out I was being a big dumb, but it had nothing to do with my use of destroy. Target.DealDamage had something erroring in it because I was testing this without using networking. I didn't think it was a problem, but it turns out that when Unity finds a runtime error, it starts the loop over and thus was skipping my destroy call. The more you know!