user4829644
user4829644

Reputation:

Int is not showing in text field

My goal is to get the user to type in how much gold they have to convert into cash by multiplying the amount of gold they entered with 1000. Each goldbar is worth 1000 cash. Then I want to display what the total would be.

Also, would there be another way around using the update function to constantly update this? I feel like it would to too performance intensive.

I get this error:

FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629) NSExchangeManager.ExchangeManager.Update () (at Assets/ExchangeManager.cs:37)

I've tried to swap where the int.Parse() are converted but no luck.

if(inputText.text != null)
{
    // Get input text
    string amountOfGold = inputText.text;
    // Set gold value
    int goldValue = 1000;
    // Multiply goldvalue by amount of gold
    int total = goldValue * int.Parse(amountOfGold);

    // Show the total in the 'money text'
    money.text = "$" + total.ToString();
    // Show amount of gold typed
    gold.text = amountOfGold;
}

Upvotes: 0

Views: 155

Answers (2)

user4829644
user4829644

Reputation:

Thanks a lot for the help! This is what finally has worked for me.

if(inputText.text != null)
        {
            // Get input text
            string goldInput = inputText.text;
            int goldInputNum;
            int.TryParse(goldInput, out goldInputNum);

            // Set gold value
            int goldValue = 1000;

            int total = goldValue * goldInputNum;

            // Show the total in the 'money text'
            money.text = "$" + total.ToString();
            // Show amount of gold typed
            gold.text = goldInput;
        }

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

Input string was not in the correct format

Means that the value of the textbox (string) is not a valid numeric value that can be parsed as int. you should use TryParse() instead:

int gold = 0;
int.TryParse(amountOfGold,out gold);
int total = goldValue * gold;

Upvotes: 3

Related Questions