Can't add a value to HashSet<T> in edit mode / dirtying it does nothing

I have a scene with a game object that has my component.
The component has a HashSet (doesn’t matter what T is) and I want to add a value to it (via a button in custom inspector Editor) but it doesn’t keep when pressing Play and back. I assume the EditorUtility.SetDirty( comp ); doesn’t affect it since its not serializable.

But I do need a solution for this. I thought about making a special List that has ONLY unique values (that’s why I am using HashSet) but perhaps one of your gurus have a fix for my problem.

I am aware that EditorUtility.SetDirty will be depracated at one point, I don’t want to use FindProperty because it has a string parameter and my stuff gets renamed and I’d have to make sure it has the same name as the string >.>

It’s a pain, but Unity’s serialization is a pain in general. You COULD do something like this:

[Serializable]
public class HashSetObject : HashSet<SomeSerializableBaseClass> {};

It’s a dirty, ugly, horrible hack, but it should work. I’ve learned to avoid relying on Unity’s serialization in general if at all possible.

Sort of works mate, except that I would like to have it work by since I will be using this in many places with several different types of
This [Serializable] public class SerializableHashSet : HashSet {} does not work :<

Yup, Unity’s serialization hates generics. And Interfaces. And all those handy things that make our lives easy.

1 Like

I am going to try to extend list and override add to force unique values, then overriding the inspector to have it look like a Set instead of an ordered list