- Home /
How to random generate stat for different characters?
I have a problem with random generate stat for the character. I am still new in using Unity engine. I hope that someone can help me. There is no error in the code but i when i play the game the character stat are all zero and i notice that unity have an error start() cannot take parameter. Please someone help me.
private void Start(int remainingStat, int useStat)
{
//random stat for guildrecruit
remainingStat = totalStat - useStat;
int health = Random.Range(3, remainingStat);
int attack = Random.Range(3, remainingStat);
int defense = Random.Range(3, remainingStat);
int accuracy = Random.Range(3, remainingStat);
int evasion = Random.Range(3, remainingStat);
int speed = Random.Range(3, remainingStat);
int cunning = Random.Range(3, remainingStat);
totalStat = 30;
useStat = health + attack + defense + accuracy + evasion + speed + cunning;
}
is it possible to code to allow allocation of stat to random different number of stat ins$$anonymous$$d of 7 stats? Because I want unity engine to place stat randomly.
Example:
health = 3 attack = 12 defense = 3 accuracy = 3 evasion = 3 speed = 3 cunning = 3
health = 4 attack = 4 defense = 7 accuracy = 6 evasion = 3 speed = 3 cunning = 3
and etc.
If it is possible, might I know a better solution? because I check out this question, https://answers.unity.com/questions/844863/c-random-stats-distribuition.html The author use array ins$$anonymous$$d of my poor written code. Sorry for the barrage of question. I really want to code better. Sorry.
Answer by ImpOfThePerverse · May 18, 2018 at 03:44 AM
One problem is that you're declaring new stats within the Start block when you put "int" in front of health, attack, etc. I'm assuming those stats are already variables in the class? If so, get rid of the "int" in front of each. As for the error message, void Start() is one of the stock functions on Monobehaviours. It's where you would want to put this kind of thing (variable initializations) but it doesn't take any parameters, so you should give your function a different name and call it from Start().
Answer by unity_YBW262B1IWmeXw · May 18, 2018 at 03:52 AM
I have solve the start() parameter problem but when I start the game, all the basevalue for each stat is 0. I know I m a Noob in coding but can you show me a proper way to code in order for all the stat (health, attack, defense, etc.) to be a total 30 points where the minimum points for each stats is 3.
P.S: There are no limit for the stat.
Something like this:
int health;
int attack;
int defense;
int accuracy;
int evasion;
int speed;
int cunning;
private void AllocateStats()
{
float health$$anonymous$$ult = Random.value;
float attack$$anonymous$$ult = Random.value;
float defense$$anonymous$$ult = Random.value;
float accuracy$$anonymous$$ult = Random.value;
float evasion$$anonymous$$ult = Random.value;
float speed$$anonymous$$ult = Random.value;
float cunning$$anonymous$$ult = Random.value;
float multTotal = health$$anonymous$$ult + attack$$anonymous$$ult + defense$$anonymous$$ult + accuracy$$anonymous$$ult + evasion$$anonymous$$ult + speed$$anonymous$$ult + cunning$$anonymous$$ult;
health = 3 + (int)((health$$anonymous$$ult / multTotal) * (30 - 7 * 3));
attack = 3 + (int)((attack$$anonymous$$ult / multTotal) * (30 - 7 * 3));
defense = 3 + (int)((defense$$anonymous$$ult / multTotal) * (30 - 7 * 3));
accuracy = 3 + (int)((accuracy$$anonymous$$ult / multTotal) * (30 - 7 * 3));
evasion = 3 + (int)((evasion$$anonymous$$ult / multTotal) * (30 - 7 * 3));
speed = 3 + (int)((speed$$anonymous$$ult / multTotal) * (30 - 7 * 3));
cunning = 3 + (int)((cunning$$anonymous$$ult / multTotal) * (30 - 7 * 3));
}
Note that rounding error will make it so that the sum isn't exactly 30. If you want better accuracy, use floats for stats, or make the stat range more like 30 to 100 rather than 3 to 10. Also, with 7 stats all starting out at 3 each, and 30 points to divide between them, you only end up with 30 - 7 * 3 = 9 total stat points to divide between 7 stats, so they'll all end up being 3, 4, or 5.
Dear Impoftheperserve,
is it possible to code to allow allocation of stat to random different number of stat ins$$anonymous$$d of 7 stats? Because I want unity engine to place stat randomly.
Example:
health = 3 attack = 12 defense = 3 accuracy = 3 evasion = 3 speed = 3 cunning = 3
health = 4 attack = 4 defense = 7 accuracy = 6 evasion = 3 speed = 3 cunning = 3
and etc.
If it is possible, might I know a better solution? because I check out this question, https://answers.unity.com/questions/844863/c-random-stats-distribuition.html The author use array ins$$anonymous$$d of my poor written code. Sorry for the barrage of question. I really want to code better. Sorry.
The second method posted in the answer there is a better way to do it (it occurred to me last night after posting my answer to it, it's a way to end up with exactly the right stat total.) The only difference between that and what you want is that it caps the stats at 6 max. Here's a modified version that removes that restriction: public void randomStats() { int[] stats = {3, 3, 3, 3, 3, 3, 3};
while (Sum(stats) < 30) {
int randomIndex = Random.Range(0, stats.Length);
stats[randomIndex]++;
}
health = stats[0];
attack = stats[1];
defense = stats[2];
accuracy = stats[3];
evasion = stats[4];
speed = stats[5];
cunning = stats[6];
}
private int Sum(int[] stats) {
int sum = 0;
for (int i = 0; i < stats.Length; i++) {
sum += stats[i];
}
return sum;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612162909im_/https://answers.unity.com/themes/thub/images/avi.jpg)