Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kris_absolution · Sep 12, 2019 at 05:22 PM · updatebooleanif-statements

Making a level up button.,

I'll try and explain as best I can, still think of myself as a beginner. So I have created a button that appears upon attaining a number of experience points, clicking the button causes the level up points(hp etc) to be applied and then the button disappears only to be called again upon next level up.

===========================================================

 public class GlobalLevel : MonoBehaviour
 
 
 {
 
 
    public static int currentLevel = 1;
     public int InternalLevel;
 
 
 
 public bool levelupDeActive = false;
     public bool gainedLevel = false;
     public GameObject levelupBtn;
     public GameObject buttonPanel;
 
     // Update is called once per frame
     void Update()
     {
         InternalLevel = currentLevel;
 
       
      if (GlobalXp.currentXp >= 20)
       {
 
             currentLevel = 2;
 
             if (levelupDeActive == false)
             {
               
                 buttonPanel.SetActive(true);
 
             }
         }
        
 
         if (GlobalXp.currentXp >= 40)
         {
             currentLevel = 3;
             gainedLevel = true;
 
             if (levelupDeActive == false)
             {
 
                 buttonPanel.SetActive(true);
 
             }
 
         }
 
         if (gainedLevel == true)
         {
             levelupDeActive = false;
         }
         
     }
 
     public void LevelUP()
     {
        
         PLayerPerks.PerkPoints += 1;
         HealthMonitor.MaxHealth += 50;
         //  GlobalXp.currentXp += 1;
         levelupDeActive = true;
         buttonPanel.SetActive(false);
 
        
        
        
        
     }
 
    
 }

==============================================================

It works fine for the first level up (level 1 to level 2) but the problem is that once it is turned to false within update, upon obtaining enough xp for level 3, it simply keeps setting the bool back every frame. I need a way of

if levelup has been achived(xp has reached 40) thepanel becomes active until the button is clicked click button, gain stats,now stay inactive until next xp milestone.


First time asking a question so sorry if its a bit all over the place, kinda hoping that typing this out I might figure it out myself :P

thanks for any help.

,

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 kris_absolution · Sep 12, 2019 at 05:23 PM 0
Share

I'm sorry about the post format, no idea what I did :P

2 Replies

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

Answer by sadowlight123 · Sep 13, 2019 at 10:49 AM

I wrote for you a more general technique for levels.

  • Make a custom data type (class) called level , that will include all properties (that are the same for all levels).

  • Make a list of level called levels

  • Each time check for the level that is your currentlevel + 1 for the neededXP

  • Once the XP is >= then my currentlevel + 1 neededXP , then show the panel Once the button pressed level up as usual.

The draft code:

   public class button : MonoBehaviour
     {
         public int currentXp;
         public int currentLevel = 1;
         public GameObject levelupBtn;
         public GameObject buttonPanel;
 
         [System.Serializable]
         public class level {
             public int number;
             public int neededXP;
             //add all needed properties of a level maybe like combat points, etc.
         }
     
         public List<level> levels;
     
         // Update is called once per frame
         void Update()
         {
             checkStates();
         }
     
         void checkStates()
         {
             if (currentLevel < levels.Count)
             {
                 if (currentXp >= levels[currentLevel].neededXP)
                 {
                     buttonPanel.SetActive(true);
                 }
             }
         }
     
         public void LevelUP()
         {
             //  do whatever you want here
             currentLevel++;
             buttonPanel.SetActive(false);
     
         }
     }


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

Answer by kris_absolution · Sep 13, 2019 at 01:01 PM

I thought about it for a while and actually managed to fix it myself:

   If (GlobalXp.currentXp >= 20 && GlobalLevel.currentLevel == 1)
   {
 
         buttonPanel.SetActive(true);
     }
    
 
     if (GlobalXp.currentXp >= 40 && GlobalLevel.currentLevel == 2)
     {
         buttonPanel.SetActive(true);
 
     }
 
  
     
 }
 
 public void LevelUP()
 {

 //increase level after button is clicked so its not repeated in update.
         GlobalLevel.currentLevel += 1;
         PLayerPerks.PerkPoints += 1;
         HealthMonitor.MaxHealth += 50;
            
         buttonPanel.SetActive(false);
     
        
        
        
        
     }


Probably not as efficient but I'm learning \0/.

Thanks for the reply though, I will probably convert it to something similar later on when I have learned more about (lists) etc.

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

114 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

Related Questions

Coroutines and if statements 1 Answer

What am I doing wrong with this bool? 3 Answers

Update collisions 1 Answer

Most efficient way of pausing a function 0 Answers

For Loop Doesn't Appear to be running 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