Trying to destroy a destroyed object

Hello there, i have a 3d game and my function is called when a unit dies

  public override void Die()
    {
        base.Die();
        if (gameObject != null)
        {
            Destroy(gameObject);
            if (buttonSpawned)
            {
                spawner.GetComponent<BuyUnits>().amountOfUnits -= 1;
            }
        }
    }

It happens when more are attacking a single unit and i guess it is not a problem but the inspector shows me error that im trying to access a destroyed object and navigates me to this line
if (gameObject != null)

after destroy, gameobject = null should solve

A most probably clearner way to solve your issue would be to not directly destroy your object.
Instead have a flag for your current “dead status” so some bool isDead = false; which you set to true in your Die() function.

then do something like that:

public override void Die()
 {
     base.Die();
     if (gameObject == null || isDead) return;
     isDead = true;
     if (buttonSpawned)
     {
         spawner.GetComponent<BuyUnits>().amountOfUnits -= 1;
     }
     StartCoroutine(delayedUnityCleaner());
 }

 IEnumerator delayedUnitCleaner()
 {
       yield return null;
       Destroy(gameObject);
 }

As an alternative to using a coroutine you could also just put that check in the LateUpdate();
That way your Die() can be called as many times as you want without becoming an issue.

What is in your base.Die() method? There could be something there affecting this. Could you please send that code?