- Home /
How do I make my total add up each time I roll a dice's side?
Hello,
I am trying to get my assignment done for my class. While my teacher has been encouraging me, he has been mostly unhelpful since this assignment is "figure it out yourself" type...
I have seriously no experience in programming; however I managed to get like 80% done.
I am trying to get the total added up every time you roll a dice and whatever side you get, gets added to the total. Like side 11 was your total; roll the dice again and your side will be 12 which adds up the total to 23.
Here's the Dice roll UI along with part of the script.
Answer by tormentoarmagedoom · Sep 04, 2018 at 07:11 AM
Good day.
I recommend you to have a int variable to store the acumulated value, where you will add all results.
int totalNumber;
Once you have one dice result, you add it to the total with this:
totalNumber = totalNumber + newNumber;
or this, that is Exactly the same:
totalNumber+=newNumber;
So every time you have a dice result, it will be added to the total. Then only need to show in the text the total.
Bye!!
Yes, this should solve the problem, just one clarification: the totalNumber
variable should be a member variable of the class, so that it is "remembered" (I'm sure this is what tormentoarmagedoom meant).
BTW, if your die is to be 20-sided die, Random.Range(1, 20)
is incorrect: Random.Range(int $$anonymous$$, int max)
generates a number between $$anonymous$$
and max
, including $$anonymous$$, but excluding max. So for a 20-sided die roll you need to increase max
by one.