I'm having trouble subtracting in C#

62 Views Asked by At

So I'm trying to subtract one integer value from another integer value, and I can't seem to get it to work?

Example of my problem,

int enemyHP = 10;
int playerDamage = 10;
//The player decides to attack the enemy and deals (playerDamage) damage to the enemy

enemyHP - playerDamage //Doesn't work, and I can't find anything else that works.

I know I can do enemyHP-= 10; but that isn't what I need, because playerDamage can be changed in the game, so it isn't always going to be a fixed number.

1

There are 1 best solutions below

0
Iraj On

In your example, you can update the enemy's HP by subtracting the player's damage from it using the following code:

enemyHP = enemyHP - playerDamage;

Or

enemyHP -= playerDamage;

Or you can define a new variable and put the result on that:

int result = enemyHP - playerDamage;