No problemo
Don’t actually try and save the material. It is possible, but it’s not a great way to do it.
What works much better, is to put your materials into an array, and just save the index to the array. Just an integer.
Can you show me a bit of code where you change the material? Then I can show you how to get them in an array of the right type.