Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
This question was closed Mar 11, 2017 at 02:28 PM by GamezAtWork for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by GamezAtWork · Mar 11, 2017 at 02:19 PM · custom-inspectorcustom-editor

Custom Inspector Array Elements not saving

So, I'm trying to create a Custom Inspector for an array of custom classes, and I would like to have the ability to change the class of the array element based on a dropdown list. In my case, I have an array of the base class (Command), and I would like to be able to change the element to either a TestCommand instance or a TestLongCommand instance (both of which inherit from Command). The problem now is that when I change the elements, THE VALUE IS NEVER SAVED. Even after I set them to dirty, and set them serializable. So even if I create a list which has a 3 TestCommands and 2 TestLongCommands, they all become 5 Commands when I reload the scene/press play. Could somebody please help me out? I've been searchin for like 4 hours and haven't been able to find the solution to my problem...

CommandList.cs

 [System.Serializable]
 public class Command {
 
     public virtual string commandName{
         get { return this.GetType().Name;}
     }
     ...
 }
 
 [System.Serializable]
 public class CommandList : MonoBehaviour {
     public List<Command> commands;
     public int currCommandIndex = 0;
         ...
 }

CommandListEditor.cs

 [CustomEditor(typeof(CommandList))]
 public class CommandListEditor : Editor {
 
     CommandList script;
 
     void OnEnable()
     {
         script = (CommandList)target;
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         EditorGUILayout.PrefixLabel("Commands");
         SerializedProperty commandsProp = serializedObject.FindProperty("commands");
 
         List<string> allTypeStrings = new List<string>();
         System.Type[] allTypes = Methods.GetSubclasses<Command>();
         foreach(System.Type type in allTypes)
         {
             allTypeStrings.Add(type.Name);
         }
 
         int i = 0;
         foreach(SerializedProperty childProp in commandsProp)
         {
             EditorGUILayout.LabelField("[" + i.ToString() + "]");
             //Debug.Log(currValue);
 
             int currIndex = -1;
             if (script.commands[i] != null)
             {
                 for (int j = 0; j < allTypeStrings.Count; ++j)
                 {
                     if (allTypeStrings[j] == script.commands[i].commandName)
                         currIndex = j;
                 }
                 EditorGUILayout.LabelField(script.commands[i].commandName);
             }
 
             int newIndex = EditorGUILayout.Popup(currIndex, allTypeStrings.ToArray());
             if (newIndex != currIndex)
             {
                 Debug.Log("Changed from " + ((script.commands[i] != null)?script.commands[i].commandName:"NULL") + " to " + allTypeStrings[newIndex] + " in " + serializedObject.targetObject.name);
 System.Activator.CreateInstance(allTypes[newIndex]) as Command);
 
                 script.commands[i] = System.Activator.CreateInstance(allTypes[newIndex]) as Command;
 
             }
 
             childProp.serializedObject.ApplyModifiedProperties();
             commandsProp.serializedObject.ApplyModifiedProperties();
 
             ++i;
         }
 
         if (GUI.changed)
         {
             EditorUtility.SetDirty(script);
             EditorUtility.SetDirty(script.gameObject);
             Undo.RecordObject(script, "Changed command");
             EditorSceneManager.MarkSceneDirty(script.gameObject.scene);
 
             Debug.Log("CHANGED!");
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 
 }
 
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by GamezAtWork · Mar 11, 2017 at 02:27 PM

http://answers.unity3d.com/questions/14877/can-a-custom-inspector-serialize-a-list-of-derived.html

facepalm

Changing it to ScriptableObject and segregating it into different files solved it.

The funny thing was that I DID try it a while back but it didn't work, and I have no idea why.

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 muhammad_ali_safdar · Apr 12, 2019 at 11:11 AM 0
Share

c# property dont show and not save elements values in unity. https://answers.unity.com/questions/609468/how-to-c-public-variable-not-show-up-in-the-inspec.html

Follow this Question

Answers Answers and Comments

65 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

No type in ObjectField created in uxml (UIElements) 3 Answers

Unable to edit fields from custom inspector 1 Answer

Objects in Custom Editor Resetting on Play 1 Answer

EditorGUILayout.Popup with an array of GameObject? 0 Answers

Making a custom editor window much like the Animator window? 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