Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 MrCrankyRooster · Jan 14, 2021 at 04:10 PM · levels

Is there anyway to shorten this code?

private void Start() { experienceStatic = PlayerPrefs.GetInt("experience");

     if (playerLevel == 1) 
     {
         PlayerHealth.maxHealth += 1;
     }
     if (playerLevel == 2) 
     {
         PlayerHealth.maxHealth += 2;
     }
     if (playerLevel == 3) 
     {
         PlayerHealth.maxHealth += 3;
     }
     if (playerLevel == 4) 
     {
         PlayerHealth.maxHealth += 4;
     }
     if (playerLevel == 5) 
     {
         PlayerHealth.maxHealth += 5;
     }
     if (playerLevel == 6) 
     {
         PlayerHealth.maxHealth += 6;
     }
     if (playerLevel == 7) 
     {
         PlayerHealth.maxHealth += 7;
     }
     if (playerLevel == 8) 
     {
         PlayerHealth.maxHealth += 8;
     }
     if (playerLevel == 9) 
     {
         PlayerHealth.maxHealth += 9;
     }
     if (playerLevel == 10) 
     {
         PlayerHealth.maxHealth += 10;
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Maypher · Jan 14, 2021 at 04:23 PM

If you're adding the same amount of health as the level your character's at (if at level 6, add 6 HP) You can simplify your code to this:

 PlayerHealth.maxHealth += playerLevel

This way no matter which level you're on it will add that amount of health

Comment
Add comment · Show 7 · 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 MrCrankyRooster · Jan 14, 2021 at 05:00 PM 0
Share

thanks, any way to shorten the levels as well?

avatar image MrCrankyRooster MrCrankyRooster · Jan 14, 2021 at 05:02 PM 0
Share

sorry I didnt include that part.. oops

if (experience >= 10) { playerLevel = 1;

     }
     if (experience >= 50)
     {
         playerLevel = 2;
     }
     if (experience >= 100)
     {
         playerLevel = 3;
     }
     if (experience >= 200)
     {
         playerLevel = 4;
     }
     if (experience >= 300)
     {
         playerLevel = 5;
     }
     if (experience >= 500)
     {
         playerLevel = 6;
     }
     if (experience >= 1000)
     {
         playerLevel = 7;
     }
     if (experience >= 1500)
     {
         playerLevel = 8;
     }
     if (experience >= 2000)
     {
         playerLevel = 9;
     }
     if (experience >= 3000)
     {
         playerLevel = 10;
     }

any way to shorten this as well?

avatar image Maypher MrCrankyRooster · Jan 14, 2021 at 05:19 PM 1
Share

I created 2 functions that will simplify that code and the original question's. So you'll only need one script to handle both values.

     int currentXP = 0;
     int xpToNextLevel = 100; //This can be anything, it's the required XP to advance to the next level.
     int currentPlayerLevel = 1;
     int $$anonymous$$axHealth = 10; //Base health at level 1

     void addExperience(int amount)
     {
         currentXP += amount;
         if (currentXP >= xpToNextLevel)
         {
             levelUp();
         }
     }
     void levelUp()
     {
         xpToNextLevel = 200; //Set the required XP to advance again
         currentXP = 0; //Reset XP
         currentPlayerLevel += 1; //Add 1 level to the player
         $$anonymous$$axHealth += currentPlayerLevel;
     }

Now you just need to call the "addExperience" function whenever you want to give your character XP. Note this doesn't handle multi-level upgrade, so if you go up 3 levels at once you'll have to figure out how to handle that.

Show more comments
avatar image
0

Answer by Llama_w_2Ls · Jan 14, 2021 at 04:20 PM

PlayerHealth.maxHealth += playerLevel;

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.

Follow this Question

Answers Answers and Comments

112 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 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

How do I change scenes from triggers in UNity 1 Answer

Unity game level design patterns 0 Answers

How to add multiple levels? 3 Answers

players create level 0 Answers

Multiple Scenes for Multiple Levels, or One Scene with all Levels? 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