在 Unity 中,我如何停止永久数学语句?

In Unity how do I stop perpetual math statements?

问题很简单,但我一辈子都弄不明白。

我的逻辑是这样的

// Static floats are StatBase.maxHealth = 0 and rStat.maxHealth = 70

class rStat : Monobehaviour
{

    
    public bool nomatter = false;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            nomatter = true;
        }


        if (nomatter == true)
        {
            healthcalc();
        }

        void healthcalc()
        {
         StatBase.maxHealth += rstat.maxHealth; // StatBase.maxHealth should = 70 but the 
                                                // number never stops adding

         nomatter = false;

        }
    }
}

说实话逻辑很奇怪

如果你已经 一个你想要采取行动的标志,为什么还要有这个 bool 标志?你可以简单地做

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        healthcalc();
    }
}

// in general rather put this on class level and don't nest it under Update
void healthcalc()
{
     StatBase.maxHealth += rstat.maxHealth; 
}

或者如果只有一行甚至

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        StatBase.maxHealth += rstat.maxHealth;
    }
}