Home > database >  In Unity how do I stop perpetual math statements?
In Unity how do I stop perpetual math statements?

Time:11-25

The question is simple but I can't for the life of me, figure it out.

My logic goes like this

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

        }
    }
}

CodePudding user response:

To be honest that logic is quite strange.

Why have this bool flag if you already have one you want to act on? You can simply do

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

or if there is only one line anyway even

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