- Home /
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.
Answer by Bunny83 · Mar 17, 2015 at 06:08 AM
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.
I am sorry, here are both scripts, maybe you can understand better! Also thanks for the answers, it gave me some background.
PlayerStats: http://pastebin.com/A4aWSkRw PlayerController: http://pastebin.com/hhmPjirG
Thanks for the attention and if you have any tips how to improve my scripts, I also appreciate it!
Edit: I was gonna pass them by ref but after some research on ref, I would rather return the value!
@Paffedd On the Pasebin site it shows "This paste has been removed!".
If you want to return the value -
public void DecreaseStat(float value, float value1)
{
return value -= value1;
}
Utilizing Decrease stat method -
float updatedEnergy = PlayerStats.Instance.DecreaseStat(_playerEnergy,_playerProjectileEnergyCost);
UpdateEnergyUI(updatedEnergy );
Thank you and sorry, I will repost so you can have a look! Also you forgot to change void to float. But yes I changed them to return ins$$anonymous$$d of passing with the ref keyword.
PlayerController: http://pastebin.com/XWYGp6yv PlayerStats: http://pastebin.com/FhFb1QU4
Thanks so much for the attention and please if you take a look at my code and you think there is better ways for my code approachs, I would love to know!
Your answer
Follow this Question
Related Questions
Date File was edited? 0 Answers
Unity UI Builder Toggle can't change checkmark size 1 Answer
Angle between vectors - calculation 2 Answers