Refactoring properties to be inside a new class, but keeping old assigned values?

Hi all,

Assume I have a serializable class like this:

class Foo {
    public float a;
    public float b;
}

And I want to refactor it to something like this:

class Bar {
    public float a;
    public float b;
}

class Foo {
    public Bar bar;
}

Is there any way I can maintain whatever was assigned to a and b in inspector for all objects, ideally as transparently as possible? (I’m thinking something like FormerlySerializedAs).

The reason I’m asking is it’s for an asset update, where I’d like to consolidate some variables and functionality so I can use it in a new place as well. However, I absolutely don’t want to risk anyone updating losing any work. Any and all ideas would be very much welcome!

Rename Foo to Bar (First in Unity, then in your IDE). Your field values should stay the same because the rename preserved the script Asset GUID. Then create a new Foo script.

Sorry, I appreciate your answer but I may have made my sample a bit too abridged. Foo contains a lot more than just a and b. Unless I’m missing something this solution also wouldn’t work for anyone updating my asset through the Store, as that would be a single-step change?

Ah ok, I didn’t realize this was a published asset in the asset store! Yeah you’ll need something more complex possibly. You might need to create a Foo2 and a script that migrates the old data, or something along those lines.

No worries! I was afraid it’d turn into something hairy like that. In that case I think I may just go with the slightly-less-elegant, lot-less-headaches solution of making Foo extend Bar as much as possible. :stuck_out_tongue: Thanks again for your input!