Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cannot implicitly convert int[] to float when creating an EXP bar.

Discussion in 'Scripting' started by Pocknell-r251, Jun 22, 2020.

  1. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    Hello, I'm new to creating games. I've been following a tutorial on creating a 2D RPG in Unity. The tutorial showed how to create a health bar, but not an experience bar (though it does show creating experience).
    I've been trying to create an EXP bar myself, to see if I could.
    I keep getting the error "cannot implicitly convert int[] to 'float'"

    Code (CSharp):
    1. expBar.maxValue = playerExp.toLevelUp; // returns an error, refering to how much exp is needed to level
    2.         expBar.value = playerExp.currentExp; // refering to how much exp the player currently has
    3.         expText.text = "Exp: " + playerExp.currentExp + "/" + playerExp.toLevelUp; // the text I want to display to show progress to the next level
    I can't seem to set the maxValue of the expBar to display how much exp the player needs to progress to the next level in the same way that I have to display the players max health:

    Code (CSharp):
    1. healthBar.maxValue = playerHealth.playerMaxHealth;
    2.         healthBar.value = playerHealth.playerCurrentHealth;
    3.         hpText.text = "Health: " + playerHealth.playerCurrentHealth + "/" + playerHealth.playerMaxHealth;
    The exp toLevelUp is stored in an int[] array, which changes every time the player levels up (so level one = 20 exp needed, level 2 = 50 exp) and I just don't know how to convert this to get it to display in a way that it can be used for expBar.maxValue.

    Any help will be greatly appreciated, this is my first time asking for help, first time making a game and first time coding a project.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    I'm only guessing, because you didn't share how you store these values for real. I know you wrote down something in text, but you can be mistaken since you have difficulties to address an array. So it usually best to present the entire script to be able to diagnose your problem.
    So, based this guess it should be something like this:
    Code (CSharp):
    1. expBar.maxValue = playerExp.toLevelUp[playerExp.currentLevel+1];
    Or something like this. I guess you have something in the playerExp which refers to the current level.
    Also make sure you put an if around this so it won't fail when you are on the highest possible level.
    Code (CSharp):
    1. if (playerExp.currentLevel < maxLevel) {
    2. [...]
    3. }
    or something like this.
     
    Pocknell-r251 likes this.
  3. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    Ah okay, thanks for trying with what I gave originally :')
    The full code for where I get the error is this:

    Code (CSharp):
    1. public class UIManager : MonoBehaviour
    2. {
    3.     public Slider healthBar;
    4.     public Text hpText;
    5.     public PlayerHealth playerHealth;
    6.  
    7.     public Slider expBar;
    8.     public Text expText;
    9.     public PlayerStats playerExp;
    10.  
    11.     private static bool uiExists;
    12.  
    13.     private PlayerStats playerStats;
    14.     public Text levelText;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         if (!uiExists)
    20.         {
    21.             uiExists = true;
    22.             DontDestroyOnLoad(transform.gameObject);
    23.         }
    24.         else
    25.         {
    26.             Destroy(gameObject);
    27.         }
    28.  
    29.         playerStats = GetComponent<PlayerStats>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         healthBar.maxValue = playerHealth.playerMaxHealth;
    36.         healthBar.value = playerHealth.playerCurrentHealth;
    37.         hpText.text = "Health: " + playerHealth.playerCurrentHealth + "/" + playerHealth.playerMaxHealth;
    38.  
    39.         expBar.maxValue = playerExp.toLevelUp[playerExp.currentLevel+1]; // refering to how much exp is needed to level
    40.         expBar.value = playerExp.currentExp; // refering to how much exp the player currently has
    41.         expText.text = "Exp: " + playerExp.currentExp + "/" + playerExp.toLevelUp; // the text I want to display to show progress to the next level
    42.  
    43.         levelText.text = "Lvl: " + playerStats.currentLevel;
    44.     }
    45. }
    I tried adding your
    Code (CSharp):
    1. expBar.maxValue = playerExp.toLevelUp[playerExp.currentLevel+1];
    It removed the error, but didn't have the desired affect in game, the expText still not displaying the maxValue of the exp needed to next level, or the current exp the player has.

    The entirety of the script used to keep track of player statistics (currently HP and EXP):

    Code (CSharp):
    1. public class PlayerStats : MonoBehaviour
    2. {
    3.     public int currentLevel;
    4.  
    5.     public int currentExp;
    6.  
    7.     public int[] toLevelUp;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (currentExp >= toLevelUp[currentLevel])
    19.         {
    20.             currentExp -= toLevelUp[currentLevel];
    21.             currentLevel++;
    22.         }
    23.     }
    24.  
    25.     public void AddExperience(int experienceToAdd)
    26.     {
    27.         currentExp += experienceToAdd;
    28.     }
    29. }
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Change to playerStat.currentLevel I see you have that.
     
    Pocknell-r251 likes this.
  5. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    (Hoping that image loads as I don't know how forums work generally)

    I inputted that and it comes out with this (Exp: 0/SystemInt32[]) rather than Exp 0/20, which would be the desired affect for level 1)
     

    Attached Files:

  6. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    I've tried to create a variable called displayLevel to convert it to a string
    Code (CSharp):
    1. displayLevel = string.Join("", toLevelUp);
    but now get the error "cannot implicitly convert string to float".
    I am so confused.
     
  7. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    this part

    Code (CSharp):
    1.   expText.text = "Exp: " + playerExp.currentExp + "/" + playerExp.toLevelUp; // the text I want to display to show progress to the next level
    2.  
    playerExp.toLevelUp is an array you cannot add it like this
    you always have to put an index playerExp.toLevelUp[ ? ] here

    Code (CSharp):
    1.   expText.text = "Exp: " + playerExp.currentExp + "/" + playerExp.toLevelUp[playerExp.currentLevel];
    2.  
     
    Last edited: Jun 22, 2020
    Pocknell-r251 likes this.
  8. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    this is what the code currently looks like with player.Exp.toLevelUp[playerExp.currentLevel]; which now outputs System:Single[] as I changed the array to a float, it was outputting System:Int32[] when the array was an Int. I want to be able to take the current relevant part of the array (say, 20 exp needed to level up on level 1, changes to 50 needed to level up on level 2) and output that.
    Is there a way to output the current part of the array?
     
  9. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    The maxValue is correct now with
    Code (CSharp):
    1. expText.text = "Exp: " + playerExp.currentExp + "/" + playerExp.toLevelUp[playerExp.currentLevel];
    And the EXP bar fills as it should, but the display is still wrong in terms of the text.
    Where is should read Exp: 0/25 it reads Exp: 0/System:Single[] and I'm just unsure how to print out the current relevant element of the array.
     
  10. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    Well I'm an idiot and didn't read that correctly at all.
    Should've read it more closely, changing the expText.text line was all I needed!

    Thanks for the help xoxo
     
  11. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    no prob, btw you can also do

    Code (CSharp):
    1.     expText.text = "Exp: " + playerExp.currentExp + "/" + expBar.maxValue;
    2.  
    since you set it above

    just remember that you cant use arrays without the brackets [ ]