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 Stealthygolem · Nov 18, 2014 at 12:37 AM · c#variablepropertycalling

Callling a variable and setting values only once, not every frame.

Hello. I feel this is a bit of a beginner question, but I couldn't wrap my head around it. I've tried using getter and setter for this, but I didn't manage to get it to work. I'm still new to a lot of coding aspects, so please bear with me on this.

What I am trying to do, is to give a character a set amount of stat points when that character reaches a new level. Then, naturally, what I want to happen is that the character only gets the stat points once, not every frame it is true that the character is that level.

My current code:

 public class CharacterLevelScript : MonoBehaviour {
 
     public int currentLevel;
     public int currentExp;
     public int statPoints;
 
     public GameObject[] enemies;
 
     void Start () {
         currentLevel = 1;
     }
     
     void Update () {
         List<GameObject> enemyList = new List<GameObject> ();
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
         foreach(GameObject enemy in enemies) {
             enemyList.Add(enemy);
         }
         
         foreach(GameObject enemy in enemyList) {
             EnemyHealth eh = (EnemyHealth)enemy.GetComponent("EnemyHealth");
             if(enemy.gameObject) {
                 if(eh.curhp <= 0f) {
                     currentExp += eh.xpGiven;
                 }
             }
         }
         if(currentExp >= 0 && currentExp <=100) {
             currentLevel = 1;
         }
         else if(currentExp >= 101 && currentExp <= 500) {
             currentLevel = 2;
         }
         else if(currentExp >= 501 && currentExp <= 1000) {
             currentLevel = 3;
         }


What I've tried working with, but failed to get it to work:

     public int currentLevel;
     public int CurrentLevel {
         get {
             return currentLevel;
         }
         set {
             statPoints += 5;
             currentLevel = value;
         }
     }
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 Stealthygolem · Nov 18, 2014 at 01:29 AM 0
Share

I have a current code solution working, but I will not post this as an answer, as I feel it might be done even neater than this. Feel free to critique.

     private bool level1 = false;
     private bool level2 = false;
     private bool level3 = false;
         //Etc...
 
         void Update() {
         if(currentLevel == 2) {
             if(!level2) statPoints += 5; level2 = true;
         }else{level2 = false;}
         
         if(currentLevel == 3) {
             if(!level3) statPoints += 5; level3 = true;
         }else{level3 = false;}
         //Etc...
         }

3 Replies

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

Answer by richyrich · Nov 18, 2014 at 01:35 AM

How about this...?

 if (currentExp >= 0 && currentExp <= 100 && currentLevel != 1)
 {
     currentLevel = 1;
     statPoints += 5;
 }
 else if (currentExp >= 101 && currentExp <= 500 && currentLevel != 2)
 {
     currentLevel = 2;
     statPoints += 5;
 }
 else if (currentExp >= 501 && currentExp <= 1000 && currentLevel != 3)
 {
     currentLevel = 3;
     statPoints += 5;
 }
Comment
Add comment · Show 3 · 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 Stealthygolem · Nov 18, 2014 at 01:39 AM 0
Share

I likes this one better than the one I did in the comments to my question. Thanks!

avatar image richyrich · Nov 18, 2014 at 02:02 AM 0
Share

Cheers mate. You could reduce code further though:

 (currentExp >= 0 && currentExp <= 100 && currentLevel != 1)

change to

 (currentExp > -1 && currentExp < 101 && currentLevel != 1)

Another issue with the code is that it runs every update. Ins$$anonymous$$d, when the player points increase, work out there and then how many points are needed for the next level. Then as the experience increases, you reduce the difference. When the difference is zero, you then make a call to the above code which should be in another function e.g. void PromoteCharacter().

avatar image richyrich · Nov 18, 2014 at 02:38 AM 1
Share

Depending on the project complexity, number of levels etc. we could complicate things further if you like ;)

 int[]pointsPerLevel = new int[10]{5,5,5,10,10,10,15,15,15,15};//assumes 10 levels
 
 int[]playerAdvances = new int[10]{0,100,500,1000,2000,3000,4000,5000,6000,7000};
 
 int currentLevel = 0;
 int difference = 0;
 int currentExp = 0;

//Do code that affects experience

 int increase = whatever;
 difference -=increase;
 currentExp +=increase;

//Check

 if (difference<=0 && currentLevel<9)
 {
   Promote();
 }

Then in your promotion code, assu$$anonymous$$g a zero based level (if levels start at 0 not 1) to work out the points for the next level it would be as follows:

 void Promote()
 {
   int nextLevel = currentLevel+1;
   difference+ = playerJumps[nextLevel] - playerJumps[currentLevel];
   statPoints += pointsPerLevel[nextLevel];
   currentLevel = nextLevel;
 }

Code above not tested, but you get the drift.

avatar image
1

Answer by Kiwasi · Nov 18, 2014 at 03:19 AM

I know you have acepted an answer. But that's a really ugly way to do it.

Consider this code.

  private int _currentLevel;
  public int currentLevel {
      get {
          return _currentLevel;
      }
      set {
          if (value > _currentLevel){
              statPoints += 5 * (value - _currentLevel);
          }
      _currentLevel = value;
      }
  }

A few notes

  • Wasn't sure if level can go down and you can loose stats. Ignored this possibility.

  • Added checking that the new level is actually higher then the existing level before adding stats

  • Allowed the level to increase multiple levels in one step

  • Changed the access level of the backing variable to private. You never want a public backing variable

  • If statPoints is not changed anywhere else and is totally dependent on level you could define it via a formula instead of using increments

Hope this helps. Properties are really powerful and valuable. I'd hate to see you leave them behind because you couldn't get them to work.

Comment
Add comment · Show 2 · 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 Kiwasi · Nov 18, 2014 at 03:24 AM 0
Share

Just reread your question and realised you want to do this based on experience ins$$anonymous$$d of level. The same principle applies. But you could do better with a separate class to handle experience and level.

avatar image Stealthygolem · Nov 20, 2014 at 01:50 PM 0
Share

I appreciate this answer. I will stick to the code which I gave the answer to. Because I felt it was simple enough to play with the variables. I want to be able to not have a static 5+ for the variable at every level. So, even though the code might be a bit repetitive and an overwork, it will do just fine for now. But, I really appreciate that you took the time to figure out a (possibly) better way! You're always helpful around here. :)

avatar image
0

Answer by Addyarb · Nov 18, 2014 at 01:45 AM

Doesn't get much more simple than this, you don't have to define what happens for each level. If you want to double the needed exp to level up, just do so each time the code runs.

     public int currentLevel;
 public int currentExp;
 public int statPoints;
 public int statPointsToAdd;
 public int expToLevelUp;

 void Start () {
     currentLevel = 1;
 }
 void Update(){
         if(currentExp>=expToLevelUp){
             currentExp=0;
             statPoints+=statPointsToAdd;
             currentLevel+=1;
         }
 }

}

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

29 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need to use 2 different language scripts. 1 Answer

Basic C# Public Variable Help 3 Answers

Access a JavaScript variable from a C# script? 2 Answers


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