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 /
avatar image
1
Question by RealMTG · Oct 31, 2015 at 07:24 PM · custom editorenum

Custom editor enum resets on play

Hi

I am working on getting a custom enum in my editor so I can choose a child and then when the game starts, parent a object to the object set in the enum. But every time I start the game it gets reset. I don't know what causes this to happen.

Here's my code:

 public List<GameObject> children = new List<GameObject>();
 
         int oldIndex = 0;
 
         void OnEnable()
         {
             targetScript = (FPP_Flashlight)target;
 
             foreach (Transform t in targetScript.transform)
             {
                 children.Add(t.gameObject);
             }
 
             for (int i = 0; i < children.Count; i++)
             {
                 foreach (Transform t in children[i].transform)
                 {
                     children.Add(t.gameObject);
                 }
             }
         }
 
 public override void OnInspectorGUI()
         {
             serializedObject.Update();
 
             EditorGUI.BeginChangeCheck();
             string[] names = new string[children.Count];
             int[] values = new int[names.Length];
 
             for (int i = 0; i < names.Length; i++)
             {
                 names[i] = children[i] != null ? children[i].name : "";
                 values[i] = i;
             }
 
             var index = EditorGUILayout.IntPopup("Parent", targetScript.parentIndex, names, values);
 
             if (EditorGUI.EndChangeCheck())
             {
                 targetScript.parentIndex = index;
                 Debug.Log("Set parent index to " + index);
                 targetScript.parent = children[index];
 
                 if (oldIndex != index)
                 {
                     oldIndex = index;
                 }
             }
 
             serializedObject.ApplyModifiedProperties();
         }
     }

Any help is appreciated!

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 Bunny83 · Oct 31, 2015 at 07:40 PM 0
Share

You shouldn't mix using "target" and "serializedObject". I'm not sure if that might be the problem but they most likely will work against each other. When you call "Apply$$anonymous$$odifiedProperties" the serialized object will apply the changed values inside the serialized object to the actual asset. Since you capture the values at the beginning by using serializedObject.Update(); and just apply them back at the end doesn't make much sense. You don't use the serializedObject at all so just remove them. Using "target" also makes your editor a single-object-editor. So no multiobject editing is possible.

Also "parentIndex" seems to be an integer variable and not an enum. The title is a bit confusing / misleading. You just display a custom dropdown field to select a child index as it looks like.

As long as parentIndex is actually a serialized value i don't see anything else wrong here.

If you want to use the serialized object ins$$anonymous$$d of "target" you should post your declaration of your parentIndex field in your FPP_Flashlight class.

avatar image RealMTG Bunny83 · Oct 31, 2015 at 07:49 PM 0
Share

So should I just do something like "targetScript.variable = EditorGUI..."?

avatar image Bunny83 RealMTG · Oct 31, 2015 at 08:11 PM 1
Share

Uhm, yes ^^. I'm not sure if it's still necessary to use EditorUtility.SetDirty but i guess it is. "target" is actually the old way how the inspector was handling things. Since Unity has multi selection support you might want to use serializedObject ins$$anonymous$$d. See Statements answer for more details.

That's why i asked for the actual declaration of your variable. You usually use FindProperty to get a SerializedProperty of your variable. It allows to change all instances at once when you have selected multiple instances of your object. "target" can only work on one object at a time. See CanEdit$$anonymous$$ultipleObjects and the example on the Editor docs page. They have two examples. One that uses serializedObject and one that uses "target".

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Oct 31, 2015 at 07:33 PM

 targetScript.parentIndex = index;

I think you need to set the parentIndex through a SerializedProperty for the change to stick. It's been a while since I messed around with those so I may have forgotton/made assumptions on usage.

SerializedObject
SerializedObject.FindProperty
SerializedProperty

Comment
Add comment · Show 2 · 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 RealMTG · Oct 31, 2015 at 08:24 PM 0
Share

Thanks for the help but I decided to go with @Bunny83 's solution and scrap SerializedObject. Sorry for the inconvenience.

avatar image Statement RealMTG · Oct 31, 2015 at 09:55 PM 0
Share

Hey no worries. Do what you feel most comfortable with. Although I think Bunny hints that it could be desirable to use SerializedObject for multiediting support. It'll make your editor to mainstream to the Unity user experience where you can select multiple objects and perform the same modification on all of them. Though if you don't want to support/learn that, nor support undo (I may be wrong here, but iirc you'd have to use SerializedObject or record the state of the object with the Undo class), I guess you can skip it.

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.

Follow this Question

Answers Answers and Comments

32 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

Related Questions

How can I display another enum? 1 Answer

Custom PropertyDrawer to support the Flag is not change value. 0 Answers

Custom Editors : EnumFields, Proper Use Of 0 Answers

How to create a new SerializedProperty? 0 Answers

Custom editors, enums and arrays 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