-1

I was working on a code and got an issue. What I'm trying to do is to use InputField in unity, then use that number to multiply by existing float. Here's what I got so far:

private float finePrice = 0.0001f;
public InputField enterValue;

public Text estimatedValue;

estimatedValue.text = string.Format ("{0}", finePrice * enterValue);

Error I'm getting:

Operator `*' cannot be applied to operands of type `float' and `UnityEngine.UI.InputField'

In my understanding is that I cannot multiply string (inputfield) to float? I tried changing Content Type of Input Field to "Decimal Number" but I'm getting the same error. Tried googling it, nothing to be found. Please help? I'm lost.

papi
  • 379
  • 7
  • 25

2 Answers2

4

You need to get the content of the InputField using the text property, and then convert that content to float because it's a string:

private float finePrice = 0.0001f, valueEntered;
public InputField enterValue;

public Text estimatedValue;

if(float.TryParse(enterValue.text, out valueEntered))
{
    estimatedValue.text = (finePrice * valueEntered).ToString();
} 
else
{
    estimatedValue.text = "Please enter a float value";
}

Note I've used float.TryParse so that if the user entered a value that can't be converted to float you will simply get false instead of an exception you would get if you used float.Parse. Also, I've changed your string.Format to simply ToString - There is no point of using string.Format in cases like this.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • BTW, I know nothing about unity, but I do know c#. I've simply googled `UnityEngine.UI.InputField` since the class name made it clear to me that it represents some sort of a `TextBox` and it should have a `Text` or `Content` property - which it does. Should you want to be a programmer, the single most important thing you should learn is how to use search engines. – Zohar Peled Jan 07 '18 at 05:30
  • That makes sense because it doesn't work, lol? if(float.TryParse(enterValue.Text already isn't working because code can't see reference to enterValue. At least the way you wrote it. – papi Jan 07 '18 at 05:35
  • @papi I've copied your code as the base of my own. Of course it this case there reference named `enterValue` points to `null` - I assumed you only wrote it like this so we would know what types are involved... – Zohar Peled Jan 07 '18 at 05:37
  • what do you mean? I just went using your own example as of Im trying to understand the way you've done it.... I tried all I can, thats why Im here. Asking for help. A little confusing. – papi Jan 07 '18 at 05:38
  • `enterValue` should refenrece an instance of `InputField` that actually exists in your UI, and `estimatedValue` should refenrece and instance of `Text` in your UI. They can't be just declared and used without being set first. That's very basic stuff. You should probably consult a c# tutorial. – Zohar Peled Jan 07 '18 at 05:44
  • @papi I've made a small edit to this answer, try it now. – Draco18s no longer trusts SE Jan 07 '18 at 06:02
2

Like Zohar mentioned, InputField.text is a string and you need to convert that to float before you multiply it with your other float.

I left my own answer because I think it's better to use float.Parse to make that conversion and pass in CultureInfo.InvariantCulture.NumberFormat to the second parameter as you don't know what culture the user's device is set to.

Convert InputField value to float

float inputInFloatFormat = float.Parse(enterValue.text, CultureInfo.InvariantCulture.NumberFormat);

Multiply by your float finePrice variable

float multiplyResult = inputInFloatFormat * finePrice;

Display result on your text component(Convert multiplyResult to string) then display it

estimatedValue.text = multiplyResult.ToString();
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Then you would have to wrap your `float.Parse` with a `try...catch`, while the [`float.TryParse`](https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx#Remarks) page specifically states that *"The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture."* meaning it's parsed according to the culture initialized by the device - so there's a very good chance that's the culture the user is using... BTW, there is an overload of `TryParse` that takes `NumberStyles` and `IFormatProvider` as parameters... – Zohar Peled Jan 07 '18 at 05:47
  • error CS0103: The name `CultureInfo' does not exist in the current context? What is that even? I never heard of it. Update: Oh, I had to using System.Globalization; Got it. – papi Jan 07 '18 at 05:51
  • @Programmer I'm getting an error "error CS0236: A field initializer cannot reference the nonstatic field, method, or property `mScript.enterValue'" – papi Jan 07 '18 at 05:56
  • @ZoharPeled I just realized `TryParse` has a second parameter for that so OP can use your `TryParse` but provided the second parameter from my answer or just use `Parse` in my answer but inside `try/catch`. See [this](https://stackoverflow.com/a/11203062/3785314) for why I used `NumberFormat` instead of the default one. I used that so that "." is supported. – Programmer Jan 07 '18 at 06:15
  • @papi You did not copy my code correctly. I did a test on my side and it compiles. Please try re-copying it again just like it is. Also, if that doesn't help, edit your question and add EDIT to it. Add the new code from my answer that doesn't work and I will see what you did wrong. – Programmer Jan 07 '18 at 06:21