Stack over flow

Hello, I’m getting a stack overflow when the main base is destroyed. I don’t know why it’s happening now if it’s something I may have changed or if it was corrected in a video and I missed it.


I see why, I’m wondering if it’s something I missed in the lectures.
For now, I’m just going to make a bool in the health script that if true (that it’s the unitbase), it will return so it doesn’t try to damage itself when it’s already being destroyed.

Hi there, would you like to share you UnitBase script, maybe we can figure out if there is something causing the stack overflow?

Here is the UnitBase

public class UnitBase : NetworkBehaviour, IPointerClickHandler
{
    [SerializeField] private Health health = null;
    [SerializeField] public GameObject buildingFrame = null;

    public static event Action<int> ServerOnPlayerDie;
    public static event Action<UnitBase> SeverOnBaseSpawned;
    public static event Action<UnitBase> SeverOnBaseDespawned;

    #region  Sever
    public override void OnStartServer()
    {
        health.ServerOnDie += ServerHandleDie;

        SeverOnBaseSpawned?.Invoke(this);
    }
     public override void OnStopServer()
    {
        SeverOnBaseDespawned?.Invoke(this);

        health.ServerOnDie -= ServerHandleDie;
    }

    [Server]
    private void ServerHandleDie()
    {
        ServerOnPlayerDie?.Invoke(connectionToClient.connectionId);

        NetworkServer.Destroy(gameObject);
    }

    #endregion

    #region Client

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left) {return;}

        if (!hasAuthority) {return;}

        buildingFrame.SetActive(!buildingFrame.activeSelf);
        
    }
    #endregion
}

Before Unity called this stack overflow it gave a null reference to the health script, the if client return line. This is in the Health script.

    private void SeverHandlePlayerDie(int connectionId)
    {
        if(connectionToClient.connectionId != connectionId){return;}

        if(baseBuilding == true){return;}

        PearceDamage(gameObject,currentHealth);
    }

I added the bool baseBuilding return and no longer get the stack overflow.

By the look of it, this line is being called by the UnitBase to make the health 0 on the gameobject that is already being destroyed by the the Unit base

    [Server]
    private void ServerHandleDie()
    {
        ServerOnPlayerDie?.Invoke(connectionToClient.connectionId);

        NetworkServer.Destroy(gameObject);
    }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.