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
0
Question by Azengar · Nov 06, 2018 at 04:29 PM · listinspectorserializationscriptableobjecteditorscript

Difference between assigning a value in inspector and with a custom editor script

Hello everyone, I'm running into a weird problem so thank you in advance for any help.

My question might be the same as this one but it's filled with code and has no answer: https://answers.unity.com/questions/634960/custom-inspector-saving-variables-to-its-self-but.html


I am using Unity 2018.3.0b8, if this is a known bug then I'm sorry but I did not see anything like it.

I have a list containing actions, which are ScriptableObjects saved on the disk.

 public class GameDialogueActionManager : MonoBehaviour
 {
     public List<GameDialogueAction> actions;
     ...
     // I don't use actions in the rest of the script (or anywhere else in fact)
 }


I wanted to be able to find every action defined in an assembly using Reflection, create them on the disk and then automatically add them to the action list.


So, to do this I created a custom editor script that does all of this and it's working great, the asset files are created successfully and the list is filled with all the actions (code of the custom editor is at the end).

However, I noticed that as soon as I entered play mode, the action list has all its values set to null, meaning that the list still have the same size but is filled with null values.


First I thought that the actions weren't serialized correctly, but then I tried assigning them manually using the inspector (you know drag & dropping) and everything worked fine, even in play mode.


Which leads me to the following question, is there a fundamental difference between assigning a value with the inspector and assigning it with a custom editor script that I'm missing?


Thanks again to anyone taking the time to help.

Here is my custom editor script:

 public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
         GameDialogueActionManager manager = target as GameDialogueActionManager;
 
         // Refresh and create assets on the disk
         if (GUILayout.Button("Browse all types"))
         {
             Type[] types = GetActionTypes();
 
             foreach (string file in Directory.GetFiles(ActionsAssetDir))
             {
                 File.Delete(file);
             }
 
             foreach (Type type in types)
             {
                 ScriptableObjectUtility.CreateAsset(
                     Path.Combine(ActionsAssetDir, type.Name + ".asset"), type);
             }
         }
 
         // Assign existing actions to the list
         if (GUILayout.Button("Setup Actions"))
         {
             manager.actions.Clear();
             Type[] types = GetActionTypes();
 
             foreach (Type type in types)
             {
                 ScriptableObject action = AssetDatabase.LoadAssetAtPath<ScriptableObject>
                     (Path.Combine(ActionsAssetDir, type.Name + ".asset"));
                 manager.actions.Add((GameDialogueAction)action);
             }
         }
     }



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

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

Answer by hectorux · Nov 06, 2018 at 05:37 PM

This work fine for me, try it, when you have just set them all with your button, then try copy component->paste component values. All in the same, idont know why, but with my custom editor, it doesnt keep variables well

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 Azengar · Nov 06, 2018 at 06:23 PM 0
Share

It's working, it's so easy I feel dumb haha, thanks for the tip!

I still wonder why Unity does this though.

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

106 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 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

[CustomInspector] add scriptableobject in a list always null 1 Answer

Unable to serialize my list in a Unity Custom Editor script. 1 Answer

How should I serialize data that is also editable in the Inspector? 2 Answers

Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers

How do I make child classes in a list editable from the Inspector? 1 Answer


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