• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

List of ScriptableObjects lost on project reload 1 Answer

Serialize variables of elements in a list ? 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges