I have a serializable class, like so:

public class Weapon{

    public int firerate;
    public GameObject weapon;
    public Color32 weaponColor;


(The above is just an example)

So when I try to save them to a file using BinaryFormatter I keep getting an error that GameObject and Color cannot be serialized. I tried using NonSerializable, however that makes them disappear from the inspector!

So my question is, how can I make Unity ignore to save them to a file? Or how to not serialize them but still make them show up in the inspector.

  • Why are you using a binary formatter to save this to a file? If you want to later load from that file, wouldn't you want the GameObject and Color data to be retained? If so, there might be better ways to persist this data, if you can tell us more about how you're using it in your game. – DMGregory Dec 3 at 14:56
  • If there is any other option than Binary Formatter, it would be great if you mention them! Thank you Gregory! – user122760 Dec 3 at 15:00
  • The GameObject and Color values are already set in the Inspector so I don’t want them to be saved at all. That would be useless – user122760 Dec 3 at 15:01
  • But Unity still try to save them since they are in the Weapon class – user122760 Dec 3 at 15:02
  • 3
    Don't reply with comments, edit your question to include the details requested about how you're using this data file in your game. We need that information in order to suggest a fitting solution for your application. – DMGregory Dec 3 at 15:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.