Duplicate Remains After Destroying Object
So i have run into an issue of the remains on my destroyed object being duplicated. I have it set up that when the rigid-body is destroyed it spawns a broken apart version of that object, it seems to work fine when i destroy it with damage from ray-casting, but when it is destroyed after the collision velocity it duplicates the remains. Any information is most appreciated.
private void OnCollisionEnter(Collision collision)
{
if(collision.relativeVelocity.magnitude > 10)
{
Die();
}
}
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
void Die()
{
Instantiate(remains, transform.position, transform.rotation);
Destroy(gameObject);
}
Answer by wilsonbruce43 · Feb 01, 2020 at 08:05 PM
It seems like it is reading each object collision it touches and keeps creating a new "remains," is there a way to combat that.
I would personally add a boolean IsDead and set it to true in Die() then you call Instantiate and Destroy inside a if on the IsDead,
So If(! Is Dead) { IsDead =true; Instantiate Destroy }
Found the fix for the issue by adding bool functions and running it through the "Die" code. Updated code:
private bool taskIsRunning = false;
private void OnCollisionEnter(Collision collision)
{
if(collision.relativeVelocity.magnitude > 10)
{
if(taskIsRunning == false)
{
taskIsRunning = true;
Die();
}
}
}
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
void Die()
{
Instantiate(remains, transform.position, transform.rotation);
Destroy(gameObject);
}