- Home /
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);
}
}
}
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
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
Follow this Question
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