Reputation: 1305
I am working on saving and loading options for a game and I am trying to Serialize a Vector3 but I received this error :
SerializationException: Type UnityEngine.Vector3 is not marked as Serializable.
When I look at the documentation I see Vector3 on this list of Serializable types :
Serializable types are:
- Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
My code is :
public Options_Settings GameOptions { get; private set; }
public void SaveOptionSettings(){
// Create the Binary Formatter.
BinaryFormatter bf = new BinaryFormatter();
// Stream the file with a File Stream. (Note that File.Create() 'Creates' or 'Overwrites' a file.)
FileStream file = File.Create(Application.persistentDataPath + "/OptionsInfo.dat");
// Serialize the file so the contents cannot be manipulated.
bf.Serialize(file, GameOptions); // <---- Error is here
// Close the file to prevent any corruptions
file.Close();
}
My Options_Settings :
using UnityEngine;
using System;
[Serializable]
public class Options_Settings {
public bool MusicToggle { get; set; }
public float MusicVolume { get; set; }
public bool SFXToggle { get; set; }
public float SFXVolume { get; set; }
public Vector3 UIOneScaling { get; set; }
public Vector3 UITwoScaling { get; set; }
public Vector3 UIThreeScaling { get; set; }
public void Default () {
MusicToggle = true;
MusicVolume = 0.5f;
SFXToggle = true;
SFXVolume = 0.5f;
UIOneScaling = Vector3.one;
UITwoScaling = Vector3.one;
UIThreeScaling = Vector3.one;
}
}
I know I can make a workaround and just make 3 floats for each vector and pass them around that way but I want to understand what I am doing wrong since the documentation says I can do Vector3 which would prevent extra coding and doing monotonous acts.
Upvotes: 1
Views: 1538
Reputation: 300
While unity CAN indeed serialize a Vector3 variable, it cannot serialize an ACCESSOR to a Vector3.
I see this accessor in your code:
public Vector3 UIOneScaling { get; set; }
but I don't see the ACTUAL vector variable being stored: the accessor is just a function.
I would have expected to see something like:
[SerializeField] // needed to serialize private fields
private Vector3 _UIOneScaling; //a member variable to hold the data
public Vector3 UIOneScaling {
get return {_UIOneScaling;}
set {_UIOneScaling=value;}
}
In this case the member VARIABLE _UIOneScaling WOULD be serialized.
Upvotes: 1