Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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,451
    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