• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Daniel-Everland · Dec 13, 2013 at 01:29 PM · c#guieditorinspector

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.

alt text

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);
 }

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stevethorne · Dec 13, 2013 at 04:11 PM 0
Share

Try using the SetDirty function to tell unity that the variable has been changed and needs to be saved to disk.

 EditorUtility.SetDirty( Target );


avatar image Daniel-Everland · Dec 13, 2013 at 05:53 PM 1
Share

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

avatar image OP_toss · Dec 13, 2013 at 06:09 PM 1
Share

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

avatar image Daniel-Everland · Dec 13, 2013 at 06:20 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Roland1234 · Dec 13, 2013 at 06:27 PM

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daniel-Everland · Dec 13, 2013 at 07:13 PM 0
Share

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

avatar image
0

Answer by Daniel-Everland · Dec 13, 2013 at 06:19 PM

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modifying default Inspector GUI creator 3 Answers

TexturePropertySingleLine in Editor class 0 Answers

Create Multiple Foldouts. 1 Answer

Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers

Dynamic Target for Object field (and others) in Custom Inspector 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges