【问题标题】:In Unity how do I stop perpetual math statements?在 Unity 中,我如何停止永久数学语句?
【发布时间】:2021-11-24 04:05:05
【问题描述】:

这个问题很简单,但我一辈子都想不通。

我的逻辑是这样的

// 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;

        }
    }
}

【问题讨论】:

  • heathcalc 应该在 Update 之外。
  • @Kiner_shah 这立即奏效,我非常感激。感谢您花时间回答这样一个愚蠢的问题。我仍处于学习的爬行阶段,像这样获得的一点点信息令人愤怒。
  • “数字永远不会停止添加”如何?这段代码看起来每次点击都有效。
  • 如果你点击了很多次,maxHealth 值将是 70+70+70+70 ,所以这是你的问题吗?

标签: c# unity3d if-statement static


【解决方案1】:

说实话,逻辑很奇怪。

如果您已经拥有一个您想要采取行动的标志,为什么还要拥有这个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;
    }
}

【讨论】:

  • 鼠标点击不是实际逻辑的一部分。在游戏中,玩家将单击“接受”之类的按钮来分配一个统计点,然后该统计点将增加该统计量。我的问题是它在更新方法中,所以我在做什么并不重要,它总是会主动添加。感谢您的回答,但我感谢您的帮助。
猜你喜欢
  • 2013-12-24
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 2022-06-24
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多