- Home /
How to subtract from a variable using a variable from another script
Right now I have a variable called "punchdamage" that changes based on the player's input, I know it works because I set it to display the variable's value on the debug log, but when I try to subtract a variable, Molehealth, by the "punchdamage" value, it says that the value of "punchdamage" is 0, This is my code for subtracting the Molehealth variable:
mlhealth.Molehealth = mlhealth.Molehealth - Combatmgr.punchdamage;
Also when I subtract the variable by a number, like 3f, it works.
Thank you so much in advanced!
Answer by acastro-p · Mar 31, 2020 at 02:21 AM
try adding a debug log to check if its not null when you are doing the substraction: mlhealth.Molehealth = mlhealth.Molehealth - Combatmgr.punchdamage; Debug.log(Combatmgr.punchdamage);
and try to create a method that already recive the damage value: public void take_damage(float damage){ mlhealth.Molehealth -= damage; }
the thing is that you have to consider from what object you are calling or in this case doing the damage also try making the variable static
hope this was usefull.,maybe try adding a debug log below in your code to see if its not null when you are calling it in that part of the script: mlhealth.Molehealth = mlhealth.Molehealth - Combatmgr.punchdamage; Debug.Log(Combatmgr.punchdamage);
try creating a method that requiers the value that you want to reduce public void take_damage(float damage){ mlhealth.Molehealth -= damage; }
Floats/ints/decimials (whatever value type he's using to hold the value) are value types and aren't nullable. Also, he would already be getting null reference exceptions if null-checking was at play here anyways. We need to see his punchdamage implementation to have any clue as to what may be going on.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230137im_/https://answers.unity.com/themes/thub/images/avi.jpg)