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 11:25 PM · levels

Why isn't playerLevel going up when I have enough xp?

public class HealthAndLevelSystems : MonoBehaviour {

 public int experiencePoints;
 public int playerLevel;

 public float currentHealth;
 public float maxHealth;

 public GameObject player;


 // Start is called before the first frame update
 void Start()
 {
     experiencePoints = PlayerPrefs.GetInt("experiencePoints");

     maxHealth = 10 + playerLevel;
     currentHealth = maxHealth;
 }

 // Update is called once per frame
 void Update()
 { 
     PlayerPrefs.SetInt("experiencePoints", experiencePoints);

     experiencePoints += 1;

     if (currentHealth <= 0)
     {
         Die();
     }
 }

 Dictionary<int, int> Levels = new Dictionary<int, int>()
 {
     { 50, 1 },
     { 100, 2 },
     { 150, 3 },
     { 200, 4 },

 };
 
 void SetLevel(int ExperiencePoints)
 {
     foreach (KeyValuePair<int, int> pair in Levels)
     {
         if (pair.Key == ExperiencePoints)
         {
             playerLevel = pair.Value;
         }
     }
 }

 private void Die()
 {
     Time.timeScale = 0f;
     Destroy(player);
 }

}

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 TimBur · Jan 15, 2021 at 04:37 AM 3
Share

Your SetLevel method looks solid. However, I don't see any place where you call the method. I suggest you add a call to SetLevel after every place where you change experiencePoints. For example, you could add SetLevel(experiencePoints) to line 25.

2 Replies

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

Answer by jackmw94 · Jan 15, 2021 at 10:22 AM

I think this code could be a fair bit easier to use if you have a single point at which you change your experience - this function could then update your player prefs and level:

 private const string XPSaveKey = "experiencePoints"; // add this line at the top of your class
 
 public void UpdateXP(int addXp, bool save = true)
 {
     experiencePoints += addXp;
     SetLevel ( experiencePoints );
     maxHealth = 10 + playerLevel;
 
     if ( save )
     {
         PlayerPrefs.SetInt ( XPSaveKey );
     }
 }


You can then use this function whenever you want to update the player's xp. Save will usually be true during the game except for on start. When the game starts this is the only point at which the save data will be more relevant than the game data so your start function can load the xp then call this with save as false.

For your specific issue, I think TimBur was right - you're not calling SetLevel, thus that code to increment the level is never executing. Still felt this answer was relevant since your next issue might have been that incrementing the level doesn't adjust the max health!

Comment
Add comment · Show 1 · 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 TimBur · Jan 15, 2021 at 03:12 PM 0
Share

Yup. Good points. This is a more elegant way to handle the exp/levelling process.

avatar image
1

Answer by AbandonedCrypt · Jan 15, 2021 at 01:34 PM

Its even easier and also better to have the xp needed to level up in a separate variable.

 public class Level
 {
     private int currentXp;
     private int levelUpXp;
     private int currentLevel;
 
     public void AddXp(int xp)
     {
         currentXp += xp;
         if(currentXp >= levelUpXp)
             LevelUp();
     }
 
     private void LevelUp()
     {
         currentLevel++;
         levelUpXP = GetNextLevelXP(currentLevel);
     }
 
     private void GetNextLevelXP(int level)
     {
         //look up the xp requirement for next level
     }
 }

This is by far not handling any special cases and should just demonstrate the principle. This has the added benefit of making the xp needed to level up accessible easily, so that they can be displayed next to the current XP in the character sheet.

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

113 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 avatar image

Related Questions

New scene for minigame or not? 1 Answer

Display Audio Analysis and levels with objects 0 Answers

when i click the GUI button it doesnt load the level 1 Answer

Unity Serializer Wont Load Saves 1 Answer

Load previous scene 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