Serialize Vector3

Hey there!

I am working on my saving system and I can’t deal with a serialization problem.

I’m trying to save an array of Vector3 and array of quaternions and array of Colors.
They are one dimmensional arrays.

Color[ ] colors;
Vector3 vectors;
Etc.

Unity Log says I cant because Vector3 isn’t marked as serializable.
But Unity Documentation says it is.

When I remove those arrays my whole script works. It saves and loades all ints, strings, floats etc.

What’s the problem?

Does it work in earlier versions than Unity 2019.3? Serialzation was worked on in 2019.3, perhaps you found a regression, therefore please check if this problem is new to 2019.3.

I’m using 2019.3.11b

I’ve downloaded 2019.3.0f3. The problem still exists

You can’t serialize Vector3 or any Unity Classes in a Save File.You can only serialize datatypes such as float,int,byte etc.You can also serialize their arrays.
You will have to create your own custom classes that inherits from the UnityClass you want to serialize and Add Attribute on it [System.Serializeable].
For more Info You can watch this Video.

I hope this will help!

Simple field types that can be serialized

  • Custom non-abstract, non-generic classes with the Serializable attribute
    (See How to ensure a custom class can be serialized, below.)
  • Custom structs with the Serializable attribute
  • References to objects that derive from UnityEngine.Object
  • Primitive data types (int, float, double, bool, string, etc.)
  • Enum types
  • Certain Unity built-in types: Vector2, Vector3, Vector4, Rect, Quaternion, Matrix4x4, Color, Color32, LayerMask, AnimationCurve, Gradient, RectOffset, GUIStyle

So why did they post that?

Vector3, Vector3[ ] and List<Vector3> can all be serialized, I never had a problem with that.

Please create an example script that shows what you’re doing. Please check if this works in earlier Unity versions than 2019.3, because you post this in the beta forum and I’m trying to figure out whether this is a new issue in 2019.3.

Also clarify what “serialization” means in your specific case. Do you use Unity’s serialization system or a custom solution?

The class:

[System.Serializable]
public class FragmentMeshData
{
    public int verticesPerLine = 0;
    public Vector3[] vertices = null;
    public int[] triangles = null;
    public Color[] colors = null;
    public int triangleIndex = 0;
    public int VectorIndex = 0;
    public int ColorIndex = 0;
}

My serialize method:

public static void SerializeFile<_T>(string FilePath, _T save)
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(FilePath);

        bf.Serialize(file, save);
        file.Close();

    }

I want to highlight that every other class without Vector3, Color etc. works well.
If I remove those things:

[System.Serializable]
public class FragmentMeshData
{
    public int verticesPerLine = 0;
    //public Vector3[] vertices = null;
    public int[] triangles = null;
    //public Color[] colors = null;
    public int triangleIndex = 0;
    public int VectorIndex = 0;
    public int ColorIndex = 0;
}

Code works so well

OK, so you’re not using Unity’s serialization system.

Now for the third time: Since you posted this in the beta testing forum, please figure out if this issue is related to the beta version. Does it work in Unity 2019.2 for example? If it’s not related to the beta, you have better chances to get an answer in a different sub-forum, then we can ask a moderator to move the thread, so you get help faster.

Like I said In Serialization (only) use array of float of length 3 for x,y,z instead of Vector 3
as was in the above video
Because The System doesn’t recognize Unity datatypes such as Vector 3 etc

If you don’t understand The serialization you should see this video as well

If still not The saving system of this course shows how to serialize Vector 3
https://www.udemy.com/course/unityrpg/

Thanks for those links, but I understand how it works. I’ve used it many times but always I’ve been writing my own “Vector” classes like this one:

[Serializable]
public struct Vector2S
{
    public float x;
    public float y;

    public Vector2S(float x, float y)
    {
        this.x = x;
        this.y = y;
    }

    public override bool Equals(object obj)
    {
        if (!(obj is Vector2S))
        {
            return false;
        }

        var s = (Vector2S)obj;
        return x == s.x &&
               y == s.y;
    }


    public Vector2 ToVector2()
    {
        return new Vector2(x, y);
    }

    public static bool operator ==(Vector2S a, Vector2S b)
    {
        return a.x == b.x && a.y == b.y;
    }

    public static bool operator !=(Vector2S a, Vector2S b)
    {
        return a.x != b.x && a.y != b.y;
    }

    public static implicit operator Vector2(Vector2S x)
    {
        return new Vector2(x.x, x.y);
    }

    public static implicit operator Vector2S(Vector2 x)
    {
        return new Vector2S(x.x, x.y);
    }
}

And when I use my Vector2S or similar Vector3S everything works well.

I always did it this way, but a few days ago I read that Unity allows to serialize built-in Vector3, but it seems it doesn’t.

I tried to serialize Vector3 in
2019.3.0b12
2019.3.0f1
2020.1.0a17

and it always says Vector3 isn’t marked as serializable

Where did you read that? Can Send me The Link?

1 Like

It does, but it’s slightly misleading as its referring to the built-in Unity serializer which supports Vector3 automagically.

This has existed in Unity since the beginning and is not specified to 2019.3.

Your options are;

  • Create a serializable Vector3 type (as you’ve already done)
  • Implement a serialization surrogate (as explain here)
  • Stick with the Unity serializer to store your data (ScriptableObject or JsonUtility)

This is somewhat tangential, but I think your Vector2S != operator has a bug.

Your code says they’re not equal if both x and y values are different. But only one of the values needs to be different for them to not be equal.

@Deozann thank you for your reply! Now I see, just changed && to ||

Thank you very much!

1 Like

This is a great course!