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);
}
}
}