Decrease Stat function not working

Hello,

I would like to know why my function isn’t working properly even though I am doing it correctly well!

From PlayerStats class:

	public void DecreaseStat(float value, float value1)
	{
		value -= value1;
	}

From PlayerController class:

	private void Shoot()
	{
		if(Input.GetKeyDown(KeyCode.F))
		{
			if(PlayerStats.Instance.HasEnergy(_playerProjectileEnergyCost))
			{
				/*_playerEnergy -= 25;
				_playerEnergy = Mathf.Clamp(_playerEnergy, 0.0f, _playerMaxEnergy);*/

				PlayerStats.Instance.DecreaseStat(_playerEnergy,_playerProjectileEnergyCost);
				
				UpdateEnergyUI();
				
				GameObject projectile = (GameObject) Instantiate(_projectile, transform.position, Quaternion.identity);
                //projectile.transform.SetParent(transform, false);
            }
        }
    }

It isn’t decreasing my energy at all, but if I have my DecreaseStat function on the same class and I call it normally. It actually works. Is there any explanation for it ? I would love to know, I even wondered if it could be my Singleton.

Thank you very much for the attention.

This method:

public void DecreaseStat(float value, float value1)
{
    value -= value1;
}

Is pointless as it does nothing. All method parameters are passed by value (except “ref” or “out” parameters). That means the value is copied onto the methods local stack when it’s executed. Since you don’t return the changed local value, it won’t change the value you originally passed to the method.

It’s perfectly find to call your method like this:

DecreaseStat(20, 2)

Inside your method the local “value” variable (which got the copied value “20” from the parameter) is indeed reduced by “2”, but once the method finishes the local variable is gone.

May i ask where the “_playerEnergy” variable is actually declared? From your class naming i would expect to find this variable insice the PlayerStats class, but from your code snippet above it looks like you declared it inside of your PlayerController class.

All in all this piece of code is very confusing. A bit more background might be helpful.