Search Unity

Experience bar

Discussion in 'Scripting' started by Grow, Nov 30, 2011.

  1. Grow

    Grow

    Joined:
    Apr 12, 2011
    Posts:
    335
    Hello again, i have script for a Experience bar, but experience is only +1, and i need experience +10. Please can you help me?
    Experience bar script
    Code (csharp):
    1.  
    2. var curExp : float;
    3. var maxExp : float;
    4. var level : float = 1;
    5.  
    6. function Update ()
    7. {
    8.     if (curExp >= maxExp)
    9.     {
    10.         print ("LevelUp" + level);
    11.         curExp = 0;
    12.         LevelUp();
    13.     }
    14.     curExp ++;
    15. }
    16.  
    17.  
    18. function LevelUp()
    19. {
    20.     maxExp += 20;
    21.     level ++;
    22. }
    23.  
    Thanks
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    So do curExp += 10 instead of curExp++ ? Not sure what the problem is...

    Also, your level should be an int not a float.
     
  3. Grow

    Grow

    Joined:
    Apr 12, 2011
    Posts:
    335
    Thank you very much. But now i have second problem. I have texture and when curexp + texture + and when you have level 20 texture exceeds my computer screen. And i need texture that increases ranging from 10 to 800. Perhaps you understand.
    For understand: texture start in 20 resolution screen and end in 800 resolution screen.
    Exp bar script texture
    Code (csharp):
    1.  
    2. var curExp : float;
    3. var maxExp : float;
    4. var level : float = 1;
    5. var expTexture : Texture;
    6.  
    7. function Update ()
    8. {
    9.     if (curExp >= maxExp)
    10.     {
    11.         // It means we have leveled up
    12.         print ("LevelUp" + level);
    13.         curExp = 0;
    14.         LevelUp();
    15.     }
    16.     curExp += 10;
    17. }
    18.  
    19. function OnGUI()
    20. {
    21.     GUI.DrawTexture(Rect(300, 500, curExp, 30), expTexture);
    22. }
    23.  
    24. function LevelUp()
    25. {
    26.     // Increase our max exp
    27.     maxExp += 20;
    28.     level ++;
    29. }
    30.  
    for example:

    texture increases in a range
     
    Last edited: Nov 30, 2011
  4. Kelly G

    Kelly G

    Joined:
    Oct 29, 2010
    Posts:
    35
    Not sure how your XP bar works but try changing
    Code (csharp):
    1.  
    2. GUI.DrawTexture(Rect(300, 500, curExp, 30), expTexture);
    3.  
    into:
    Code (csharp):
    1.  
    2. GUI.DrawTexture(Rect(300, 500, w *(curExp/maxExp), 30), expTexture);
    3.  
    where w= the maximum length that you want to draw the XP bar. I think maybe you said 800?
     
  5. Grow

    Grow

    Joined:
    Apr 12, 2011
    Posts:
    335
    Thanks so much