EditorGUILayout.TextField text get's deleted instantly.

I’d like to point out that this isn’t the common issue where data isn’t serialized upon playing your scene.

What happens in my case is, whatever I write into my textfield will get deleted as soon as I click elsewhere or press enter. What really boggles my mind is that this only happens in 2 out of three textfields which use almost identical code, the only difference being the variable it’s assigned to. The textfields highlighted in red below are the ones troubling me.

I’ve looked through the editor script and the target script dusins of times trying to locate my mistake, but I just cannot seem to find it. Any help would be much appreciated.

Editor Script

using UnityEngine;

using System.Collections;
using UnityEditor;

[CustomEditor ( typeof ( UpgradesDataHandler ) ) ]
public class UpgradeDataHandlerEditor : Editor {

//Private variables
private bool DamageFoldout = true;
private bool DamageUpgrade01Foldout = true;
private bool DamageUpgrade02Foldout = true;
private bool DamageUpgrade03Foldout = true;
private bool RangeFoldout = true;
private bool CooldownFoldout = true;

private UpgradesDataHandler Target { get { return (UpgradesDataHandler)this.target; } }
public override void OnInspectorGUI()
{
    if (GUI.changed)
    {
        GUI.changed = false;
    }

    object targetPlayer = target as UpgradesDataHandler;

    #region Damage Upgrade
    DamageFoldout = EditorGUILayout.Foldout(DamageFoldout, "Damage Upgrades");
	if (DamageFoldout)
	{
        if (Selection.activeTransform)
        {
            EditorGUI.indentLevel++;

            #region Upgrade 1
            DamageUpgrade01Foldout = EditorGUILayout.Foldout(DamageUpgrade01Foldout, "Upgrade 1");
            if (DamageUpgrade01Foldout)
            {
                if (Selection.activeTransform)
                {
                    #region Title
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Title");
                    Target.DamageUpgrade01Title = EditorGUILayout.TextField("", Target.DamageUpgrade01Title);
                    
                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Description
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Description");
                    Target.DamageUpgrade01Description = EditorGUILayout.TextField("", Target.DamageUpgrade01Description, GUILayout.Height(50));

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Image
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Image");
                    Target.DamageUpgrade01Image = EditorGUILayout.TextField("", Target.DamageUpgrade01Image);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Cost
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Cost");
                    Target.DamageUpgrade01Cost = EditorGUILayout.IntField(Target.DamageUpgrade01Cost);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                }
                else
                {
                    DamageUpgrade01Foldout = false;
                }
            }
            #endregion
            #region Upgrade 2
            DamageUpgrade02Foldout = EditorGUILayout.Foldout(DamageUpgrade02Foldout, "Upgrade 2");
            if (DamageUpgrade02Foldout)
            {
                if (Selection.activeTransform)
                {
                    #region Title
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Title");
                    Target.DamageUpgrade02Title = EditorGUILayout.TextField("", Target.DamageUpgrade02Title);
                    
                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Description
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Description");
                    Target.DamageUpgrade02Description = EditorGUILayout.TextField("", Target.DamageUpgrade02Description, GUILayout.Height(50));

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Image
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Image");
                    Target.DamageUpgrade02Image = EditorGUILayout.TextField("", Target.DamageUpgrade02Image);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Cost
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Cost");
                    Target.DamageUpgrade02Cost = EditorGUILayout.IntField(Target.DamageUpgrade02Cost);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                }
                else
                {
                    DamageUpgrade02Foldout = false;
                }
            }
            #endregion
            #region Upgrade 3
            DamageUpgrade03Foldout = EditorGUILayout.Foldout(DamageUpgrade03Foldout, "Upgrade 3");
            if (DamageUpgrade03Foldout)
            {
                if (Selection.activeTransform)
                {
                    #region Title
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Title");
                    Target.DamageUpgrade02Title = EditorGUILayout.TextField("", Target.DamageUpgrade03Title);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Description
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Description");
                    Target.DamageUpgrade03Description = EditorGUILayout.TextField("", Target.DamageUpgrade03Description, GUILayout.Height(50));

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Image
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Image");
                    Target.DamageUpgrade03Image = EditorGUILayout.TextField("", Target.DamageUpgrade03Image);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                    #region Cost
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.LabelField("Cost");
                    Target.DamageUpgrade03Cost = EditorGUILayout.IntField(Target.DamageUpgrade03Cost);

                    EditorGUILayout.EndHorizontal();
                    #endregion
                }
                else
                {
                    DamageUpgrade03Foldout = false;
                }
            }
            #endregion
        }
        else
        {
            DamageFoldout = false;
        }
    }
    #endregion

    EditorGUILayout.Space();

    #region Range Upgrade
    
    #endregion

    EditorGUILayout.Space();

    #region Cooldown Upgrade
    
    #endregion

    if (GUI.changed)
    {
        EditorUtility.SetDirty(target);
    }
}

}

Target Script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class UpgradesDataHandler : MonoBehaviour {

public string DamageUpgrade01Title; public string DamageUpgrade01Description; public string DamageUpgrade01Image; public int DamageUpgrade01Cost;
public string DamageUpgrade02Title; public string DamageUpgrade02Description; public string DamageUpgrade02Image; public int DamageUpgrade02Cost;
public string DamageUpgrade03Title; public string DamageUpgrade03Description; public string DamageUpgrade03Image; public int DamageUpgrade03Cost;

//Current Upgrades
public string DamageTitle_Upgrade; public string DamageDescription_Upgrade; public string DamageImage_Upgrade; public string DamageCost_Upgrade;
private int DamageLevel = 1;

public void UpgradeData()
{
    if (DamageLevel < 3)
    {
        DamageLevel++;   
    }
    UpdateData();
}
public void UpdateData()
{
    UpgradeHandler upgradeHandler01 = GameObject.Find("Upgrade01").GetComponent<UpgradeHandler>();
    switch (DamageLevel)
    {
        case 1:
            DamageTitle_Upgrade = DamageUpgrade01Title;
            DamageDescription_Upgrade = DamageUpgrade01Description;
            DamageImage_Upgrade = DamageUpgrade01Image;
            DamageCost_Upgrade = DamageUpgrade01Cost.ToString();
            break;
        case 2:
            DamageTitle_Upgrade = DamageUpgrade02Title;
            DamageDescription_Upgrade = DamageUpgrade02Description;
            DamageImage_Upgrade = DamageUpgrade02Image;
            DamageCost_Upgrade = DamageUpgrade02Cost.ToString();
            break; 
        case 3:
            DamageTitle_Upgrade = DamageUpgrade03Title;
            DamageDescription_Upgrade = DamageUpgrade03Description;
            DamageImage_Upgrade = DamageUpgrade03Image;
            DamageCost_Upgrade = DamageUpgrade03Cost.ToString();
            break;
    }

    upgradeHandler01.UpdateUpgrades(gameObject);
}

}

Try using the [SetDirty][1] function to tell unity that the variable has been changed and needs to be saved to disk. EditorUtility.SetDirty( Target ); [1]: http://docs.unity3d.com/Documentation/ScriptReference/EditorUtility.SetDirty.html

I appreciate the reply, however, if you look at the bottom of the editor script, I already do this :)

Your 3rd section also writes to "DamageUpgrade02Title" for one thing... Fix that at least. Reply if it's still broken

Haha, I actually just found that as well. Fixed it :)

2 Answers

2

The problem is on line 127:

Target.DamageUpgrade02Title = EditorGUILayout.TextField("", Target.DamageUpgrade03Title);

You are setting DamageUpgrade02Title to the new DamageUpdate03Title.

I would highly recommend you refactor those numbered properties into their own serializable class and implement a custom PropertyDrawer for them using serialized properties instead, but maybe you have reasons for doing things the way you are. At the very least having those properties grouped in a class would enable you to implement a custom editor for your target script with a reusable method for editing them: reducing complexity, making it much more maintainable, and eliminating the possibility of the kind of issue your having now.

Cheers!

Thanks, I've been looking for something like this! :)

Okay, so I found the error at line 127

Target.DamageUpgrade02Title = EditorGUILayout.TextField("", Target.DamageUpgrade03Title);

I try to parse two different variables into the third Damage Upgrade Title. This would always return DamageUpgrade02Title as null, thus resetting anything I wrote in the textfield above, while breaking the textfield for Damage Upgrade 03 Title. It’s now fully functional :slight_smile: