In the following code, I would expect that the emptyString variable is null but after entering and then exiting playmode, the emptyString variable is not null and instead an empty string.
using Debug = UnityEngine.Debug;
using UnityEditor;
public sealed class EditorWindowEmptyString : EditorWindow
{
string emptyString;
[MenuItem("Debug/EditorWindowEmptyString")]
public static void Init()
{
var window = GetWindow<EditorWindowEmptyString>();
window.Focus();
}
private void Update()
{
if(emptyString != null)
{
Debug.Log(emptyString.Length);
}
}
}```
I would too… except Unity has all these subtle serialization quirks, such as it does not support null.
Therefore, you need to inform Unity to please leave your variable alone.
Decorate it with [System.NonSerialized] and I think you’ll get what you want.
[System.NonSerialized]
string emptyString;
2 Likes
To add to the above, generally it’s best practice to check for an empty string with string.IsNullOrEmpty.
how are you setting the string from the UI?
either way instead of constantly checking if something happens in the update use the events unity provides. if you want to get a text filed content when clicking a button just run a function when the button is clicked. if you want to get the data when something is typed in then you can use the “On Value Change” or instead of waiting for the button use “End Edit”
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-InputField.html