I’ve made a system of ‘emitters’, ‘patterns’ and ‘keys’ for a top-down bullet-hell game; an emitter has an array of bullet patterns, and each pattern has an array of keys, where each key defines an emission of bullets at a certain point in time. As you can probably imagine, configuring this system with the default inspector is slightly horrifying, so I’ve been making a custom inspector and window utility to help manage it with the help of Catlike Coding’s Star tutorial and the Unity documentation.
Whilst the custom inspector operates on SerializedProperties and has no problems, I made a custom EditorWindow for editing the patterns, which operates on whatever is in Selection.activeGameobject. I’m saving the values with EditorUtility.SetDirty(); that’s fine. The snag I’ve run into is to do with the arrays.
I’ve added a few buttons to add, remove and duplicate patterns, and all these buttons operate using the ArrayUtility static functions (ie Add, Insert and so on). However, to copy patterns I had to make a custom function in my EmitPattern class, which works like this:
EmitPattern newPattern = oldPattern.Copy();
The Copy() function returns a new EmitPattern whose values have been copied over from the pattern it is called from (ie oldPattern).
With the editor window however this is leading to some errors:
- Add a new pattern using the Pattern Editor Window
- Attempt to duplicate this pattern
- Null Reference Exception error
If, however, I play the game, then try to duplicate the pattern, it works fine. What I think is happening is that the new array objects ArrayUtility creates are temporary and only saved when the game is saved/played/closed and so on, so when I try to Copy one of these ‘temporary’ EmitPattern objects, it throws the Null Reference Exception because it hasn’t been saved yet. Doing anything that causes them to be saved allows Copying to work.
So… is there any way I can make these temporary objects save in code? Or will I have to bypass ArrayUtility entirely?