TextArea not clearing its contents

Consider this code inside an editor window OnGUI :

private string m_currentCommand;
private bool m_emptyToggle;

GUILayout.Label(m_currentCommand);
m_emptyToggle = GUILayout.Toggle(m_emptyToggle, "Toggle");
if (m_emptyToggle)
{
m_currentCommand = GUILayout.TextArea(string.Empty);
}
else
{
m_currentCommand = GUILayout.TextArea(m_currentCommand);
}
GUILayout.Label(m_currentCommand);

Which is supposed to have a text area that I can clear by pressing the toggle button. For debug purposes, I display the current command text before and after the calls (in labels).

The weird thing is, that if I enable the emptyToggle button, the labels show that the string is empty, but the text area still has the text! This means that I am unable to clear the text from a TextArea even if I want to. Note : If TextArea is changed with TextField it works as expected.

Does anyone know how this can be solved?

Your code works as expected for me, except that you need to initialize m_currentCommand; e.g.

private string m_currentCommand = "";

Also, unless you really need the toggle, you could just put a button that clears it, e.g.

if( GUILayout.Button("Clear") )
  m_currentCommand = "";

I discovered earlier if that you have to set a name for say the toggle button then set focus to the the toggle then change the text it will allow you to change the text in the TextArea.

private string m_currentCommand = "";
private bool m_emptyToggle;

GUILayout.Label(m_currentCommand);
GUI.SetNextControlName("Empty_Toggle");
m_emptyToggle = GUILayout.Toggle(m_emptyToggle, "Toggle");
if (m_emptyToggle)
{
   GUI.FocusControl("Empty_Toggle");
   m_currentCommand = GUILayout.TextArea(string.Empty);
}
else
{
   GUI.FocusControl("Empty_Toggle");
   m_currentCommand = GUILayout.TextArea(m_currentCommand);
}
GUILayout.Label(m_currentCommand);

And that should fix it