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 Dracolyte · Jan 18, 2021 at 06:58 PM · bugvariableif-statements

Variable changes to a negative number somehow

Hello people of Unity Answers! I basically want to get my Level and XP system to work properly. I mean.. It works very well but I cant find the one logic error. As you can see in the script, I have coded it very simple (cuz my programming skill is not good lol) and it has only one Error. This is how the one part should be: If you level up, your current XP gets subtracted by the XP-requirement (So if the XP is larger then the required XP, the rest stays). The problem is, that if the player levels up, the XP changes to a negative number (Even if the XP is larger than the required XP). It somehow subtracts the XP twice or something... Please Help me and please tell me, how to find bugs easier. I always use Debug.Log(), are there any better techniques or something?

 private WallController wallController;
 private PlayerStats playerStats;
 public float AllCollectedXp;
 public bool CanLvlUp;
 public static LevelAndXpManager instance;
 void Start()
 {
     if (instance == null)
     {
         instance = this;
         DontDestroyOnLoad(this);
     }
     else if (instance != null)
     {
         Destroy(gameObject);
     }
     playerStats = FindObjectOfType<PlayerStats>().GetComponent<PlayerStats>();
 }
 void Update()
 {
     if (CanLvlUp)
     {
         LvlUp();
         CheckIfCanLvlUp();
     }
 }

 public void IncreaseCollectedXp(float Xp)
 {
     AllCollectedXp += Xp;
 }

 public void CheckChangeSetLvlAndXp()
 {
     ChangePlayerXp();
     CheckIfCanLvlUp();
     if (CanLvlUp)
     {
         LvlUp();
     }
 }
 
 public void ChangePlayerXp()
 {
     playerStats.playerXp += AllCollectedXp;
     AllCollectedXp = 0;
     PlayerPrefs.SetFloat("playerXp", playerStats.playerXp);
 }
 
 public void CheckIfCanLvlUp()
 {
     if (playerStats.playerXp >= playerStats.XpToLvlUp)
     {
         CanLvlUp = true;
     }
     else CanLvlUp = false;
 }

 public void LvlUp()
 {
     playerStats.playerXp -= playerStats.XpToLvlUp;
     Debug.Log(playerStats.playerXp);
     playerStats.playerLvl++;
     playerStats.XpToLvlUp += 50;
  
     PlayerPrefs.SetInt("playerLvl", playerStats.playerLvl);
     PlayerPrefs.SetFloat("playerXp", playerStats.playerXp);
     PlayerPrefs.SetFloat("XpToLvlUp", playerStats.XpToLvlUp);
 }
 

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DanielGL · Jan 18, 2021 at 07:39 PM

I guess the problem is because you are calling the LvlUp method in the Update and in the CheckChangeSetLvlAndXp method, also, your code are a little bit messy, I made a sample of a level controller with some hints of how do you can debug your code in best ways, try modify that for works in that you need and, if you have any questions just ask ;) Sample code:

 using UnityEngine;
 
 public class LevelControlerSample : MonoBehaviour
 {
     public int xpToLevelUp = 50;
 
     public int xp;
     public int level;
 
     //Hints for debug, you can create a area like that for throw your debug code
     #region DebugArea 
     public int myXpPoints;
 
     /*The ContextMenu let you call this method in the inspector(right click in your class), 
       without start the game, with this you can debug if your xp is increasing correctly*/
     [ContextMenu("AddMyXpPoints")] 
     void AddMyXpPoints()
     {
         AddXP(myXpPoints);
     }
     #endregion
 
     public void AddXP(int xpAmount)//Call this for add more xp
     {
         xp += xpAmount;
         if (CanIncreaseLevel())//The verification if it needs to up the level occurs here, so you don't need to make it on the Update method
         {
             IncreaseLevel();
         }
     }
 
     bool CanIncreaseLevel()
     {
         if (xp >= xpToLevelUp) return true;
 
         return false;
     }
 
     void IncreaseLevel()
     {
         xp -= xpToLevelUp;
         xpToLevelUp += 50;
 
         level++;
     }
 }
 
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

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

playerID does not exist in current context? 3 Answers

Why can't won't my if then statements work? 1 Answer

Weird Game Breaking Variable Bug 0 Answers

Pick up script goes straight to drop action 0 Answers

UnassignedReferenceException when its already assigned 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