Search Unity

Is it possible to limit the size of a number in an InputField?

Discussion in 'Scripting' started by carlospicomonte, Oct 23, 2021.

  1. carlospicomonte

    carlospicomonte

    Joined:
    Jan 8, 2019
    Posts:
    26
    Hello, I would like to know if there is a way to put a limit to the size of a number in an InputFiel. For example, the highest number = 30. I do not mean the number of digits ... if I try to write 31, that is not possible. I think I explain myself;) I appreciate your reply.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Check out
    InputField
    documentation... there are a few different ways to validate. What you want would probably need to live-observe the value every Update() and when it became invalid, simply instantly restore it to the previous valid value.
     
  3. carlospicomonte

    carlospicomonte

    Joined:
    Jan 8, 2019
    Posts:
    26
    Thanks for your prompt reply, as always. But I have not found anything about it in the Unity documentation, by the way, it is difficult to get clear results in searches.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    I don't think there would be any... it's just simple data validation.

    In Update():

    - keep track of what it was the previous frame (in a string)

    - every time it is different, validate it: decide is this good?

    - if yes, take this value as the previous

    - if not, restore what is in the InputField from the previous frame

    It doesn't get any simpler than that!
     
    carlospicomonte likes this.