Bug: InputField.onValueChanged always sending the previous old value and not the new in the InputField written value.
How to reproduce:
Make a script with a public onValueChanged method and connect it in unity inspector wthe Inputfield onValueChanged.
Write a log in the method and log Inputfield.text.
Test it:
0 is shown as default.
When you click in the field the log will print 0.
When you remove the value with Backspace (The cursor is not visible for unknown reasons) the log will print 0 again.
When you now type a 1 you will get a 0 in the log.
Add now a 0 to get the value 10 and the log prints 1.
Go on and add a 0 more and you will see in the log printing its always the old previous value.
But it should be the actual written value!
Or am I’m wrong?
To further analyze this issue with the InputField.onValueChanged, it sounds like you’re encountering an unexpected behavior where the event is triggered before the input field’s value has updated. This is a common behavior when working with UI inputs, particularly in Unity.
When you connect your method to the onValueChanged event, it triggers before the text is updated. Therefore, the method receives the previous value.
Suggested Solution:
You can utilize the OnEndEdit method instead of onValueChanged to ensure that you capture the finalized text after the user has finished editing. Here’s how to implement it:
Create a script and define two methods: one for onValueChanged and one for OnEndEdit.
Connect both in the Unity Inspector.
Here’s an example of the code:
using UnityEngine;
using UnityEngine.UI;
public class InputFieldHandler : MonoBehaviour
{
public InputField inputField;
void Start()
{
inputField.onValueChanged.AddListener(OnValueChanged);
inputField.onEndEdit.AddListener(OnEndEdit);
}
void OnValueChanged(string value)
{
Debug.Log("Previous Value: " + value); // This will log the old value.
}
void OnEndEdit(string value)
{
Debug.Log("Actual Value: " + value); // This will log the updated value after editing.
}
}
Instructions to Test:
Connect inputField in the Unity Inspector and test your input field.
Ensure you observe logs from both methods. The OnValueChanged method will show the old value, while the OnEndEdit method should show the correct, finalized value.
Additional Considerations:
Check the event connections in Unity. Make sure that OnEndEdit is linked properly in the Inspector.
Note that the cursor visibility issue might be related to how the input field is set up in your UI.
By following this solution, you should be able to capture the correct input value. If you have any further questions or need additional help, feel free to ask!
1 replies left - Get a PRO license for more AI replies. thread_WDKlUsQ7nVdflFthL7E6y9wd Reply to this message to continue the conversation
Hi, i think the problem is that the event is currently called before the text value is updated (will be fixed in the next update) - but the event argument itself contains the new value so you can just use that instead
// Assign this method to onValueChanged
onInputChanged(newStr:string, oldStr:string) {
console.log(newStr, oldStr);
}