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 davidflynn2 · Aug 14, 2013 at 04:51 PM · c#optimization

Shorten C# Script

I am working on create a leveling system I have this one created but it horribly long is there any way I could shorten this code down? I like how it works. Could I use an array and make it so you can type in the exp and amount of level in the inspector instead incase I decide I only want 10 levels instead of 80?

 using UnityEngine;
 using System.Collections;
 
  class PlayerStatus : MonoBehaviour 
 {
 
     public int curExperience = 1;
     public int curLevel = 1;
     
     public int lvl_1 = 10;
     public int lvl_2 = 20;
      public int lvl_3 = 30;
     public int lvl_4 = 40;
     public int lvl_5 = 50;
     public int lvl_6 = 60;
     public int lvl_7 = 70;
     public int lvl_8 = 80;
     public int lvl_9 = 90;
     public int lvl_10 = 100;
 
     
     
     public int healthlvl_2 = 20;
      public int healthlvl_3 = 30;
     public int healthlvl_4 = 40;
     public int healthlvl_5 = 50;
     public int healthlvl_6 = 60;
     public int healthlvl_7 = 70;
     public int healthlvl_8 = 80;
     public int healthlvl_9 = 90;
     public int healthlvl_10 = 100;
     
 
     
 
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
     
         if(curLevel <1)
         curLevel = 1;
         
         if(curLevel >80)
             curLevel = 80;
         
     switch(curLevel){
         case 0:
         if(curExperience <= lvl_1){
          curLevel += 0;
         
                 
          }
         break;
         case 1:
         if(curExperience >= lvl_2){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_2;
          }
        break;
         case 2:
         if(curExperience >= lvl_3){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_3;
          }
        break;
        case 3:
        if(curExperience >= lvl_4){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_4;
          }    
        break;
        case 4:
         if(curExperience >= lvl_5){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_5;
          }
        break;
        case 5:
         if(curExperience >= lvl_6){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_6;
          }
        break;
        case 6:
         if(curExperience >= lvl_7){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_7;
          }
        break;
        case 7:
         if(curExperience >= lvl_8){
          curLevel += 1;
         playerHealth.playerMaxHealth += healthlvl_8;
          }
        break;
        case 8:
         if(curExperience >= lvl_9){
          curLevel += 1;
                 playerHealth.playerMaxHealth += healthlvl_9;
          }
        break;
        case 9:
         if(curExperience >= lvl_10){
          curLevel += 1;
                 playerHealth.playerMaxHealth += healthlvl_10;
          }
        
        break;
 
             
     }
     
 
     }
 
 }
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
3
Best Answer

Answer by clunk47 · Aug 14, 2013 at 05:13 PM

EDIT: Here is a test that will allow you to assign the array values in the inspector for Health Levels. You don't need an array for Levels, you can just use an int for current level. In this example, press keypad plus '+' to increase the current level. This will debug the current level, and the max health for that level.

 using UnityEngine;
 using System.Collections;
  
 public class Example : MonoBehaviour
 {
     public int[] healthLevels;
     int currentLevel = 0;
     int maxHealth;
     
     void Update()
     {
         if(currentLevel < healthLevels.Length - 1)
         {
             if(Input.GetKeyDown(KeyCode.KeypadPlus))
             {
                 currentLevel++;
                 maxHealth = healthLevels[currentLevel];
             }
             
             Debug.Log ("Current Level - " + currentLevel + " / Max Health - " + maxHealth);
         }
     }
 }

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 davidflynn2 · Aug 14, 2013 at 05:26 PM 1
Share

I'm not sure I am following. The declaration of the levels is set up to tell how much exp is required to level and the health declaration is designed to tell how much health to add if you are on this level.

avatar image clunk47 · Aug 14, 2013 at 08:33 PM 1
Share

Edited answer.

avatar image
1

Answer by roojerry · Aug 14, 2013 at 05:39 PM

Other than level 80, for some reason, all your values are just 10 times the level. Why not just scrap all the exp and health variables and do something like:

 curLevel = (int)curExperience/10;

 playerHealth.playerMaxHealth += curLevel * 10;
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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

this script in Javascript?? 0 Answers

Game optimization using .SetActive() slowing down game. 0 Answers

How to network a large map with 4000+ movable objects using Photon Unity Networking? 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