• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_YBW262B1IWmeXw · May 18, 2018 at 12:50 AM · beginnerrandom.rangestatscharacter customization

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;
 
 
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_YBW262B1IWmeXw · May 18, 2018 at 07:02 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ImpOfThePerverse · May 18, 2018 at 04:11 AM 0
Share

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.

avatar image unity_YBW262B1IWmeXw ImpOfThePerverse · May 18, 2018 at 04:29 AM 0
Share

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.

avatar image ImpOfThePerverse unity_YBW262B1IWmeXw · May 18, 2018 at 03:14 PM 0
Share

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;
  }
Show more comments
avatar image
0

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().

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random.Range(0f, 10f) neither <= 5.5f nor > 5.5f, why/how? 1 Answer

[Beginner] Using Random.Range in initial game object position 1 Answer

Beginner please help. NullReferenceException: Object reference not set to an instance of an object 3 Answers

Beginner Help with Unity 1 Answer

Send Message? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges