How do I change my values in Input Field?

I have my Input Field which only accepts decimal numbers and a button. What I want to do is when I press the button, the number in the Input Field would half. So for example if 100 is in the Input Field and I pressed the button, I would have 50 in the Input Field. I tried finding for a method but couldn’t.

In my program, my Input Field would be in the public slot and the script would be attached to an empty child which would then be placed in On Click() for the button to access the function.

With my code below, it says that “NullReferenceException: Object reference not set to an instance of an object”. Thank you in advance for any help available.

public InputField Value;

public void Half(float half){
		Value.text = half * 0.5;
	}

That code shouldn’t even compile. You can’t multiply a string with a float. What do you think the result would be for "cat" * 0.5f ?

Does the null reference error come from that Half() method? That means ‘Value’ is null. You haven’t assigned the InputField in inspector by drag and dropping.

Other than that, you have to convert the text into a number before you can multiply.

public void Half(float half){
     float f = float.Parse(Value.text, CultureInfo.InvariantCulture);
     Value.text = (f * factor).ToString();
 }

That’s the simplest possible example. This might still throw an exception if Unity allows something in the input field that can’t be parsed to a float by float.Parse()