0
\$\begingroup\$

I have a custom data type I made called a point.

It has implicit operators to easily convert between a Vector3 and a float[], the reason being Vector3 and Vector2 types are not System.Serializable meaning they cant be converted to binary, before I would use a simple method like this:

public static void Vector3ToFloatArray(Vector3 v)
{
    return new float[] {v.x, v.y, v.z};
}

And another for float[] to Vector3

Here is the code I made for my custom point data type:

using System;
using UnityEngine;

[Serializable]
public struct Point {
    public float x, y, z;

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

    public static implicit operator Vector3(Point p) => new(p.x, p.y, p.z);

    public static implicit operator float[](Point p) => new float[] {p.x, p.y, p.z};

    public static implicit operator Point(Vector3 v) => new(v.x, v.y, v.z);

    public static implicit operator Point(float[] f)
    {
        if (f.Length != 3) throw new ArgumentException("Float array must have exactly 3 elements for point conversion.");
        return new Point(f[0], f[1], f[2]);
    }

    public bool IsZero
    {
        get {
            if (x == 0 && y == 0 && z == 0) return true;
            return false;
        }
    }
}

As you can see I have an operator for converting from Point to float array and the other way around. Yet in Unity it says:

enter image description here

It claims a Single[] can't be converted into a 'Point' even though I have the implicit operator in place.

The 'position' variable it's referring to is inside my SaveData script:

using UnityEngine;

namespace Game.Save
{
    [System.Serializable]
    public class SaveData
    {
        public int MusicIndex { get; set; } = 0;
        public Point position;

        // TEMPORARY
        public SaveData(Vector3 pos, int musicDex)
        {
            position = pos;
        }
    }
}

As you can see inside the constructer I'm able to set position to a Vector3 due to that implicit operator, so I should be able to convert a float array into it and I don't understand why it's throwing this error.

Edit: I forgot to mention the reason the error is for "point" and the struct is called "Point" is because after the error threw I also did some code optimization like making point capitalized. The new error looks like this:

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ It says point with a lower case "p". Your struct has an upper case "P". Note that C# is case sensitive. Is there another type called point somewhere? Or maybe you renamed it and there is still an older version stored somewhere? \$\endgroup\$ Feb 8 at 16:33
  • \$\begingroup\$ I have just updated my question with a small explanation on that. \$\endgroup\$
    – Pow
    Feb 8 at 16:38
  • \$\begingroup\$ Can you set a breakpoint in implicit operator Point(float[] f) to see whether it is called and then single step throught it? And also a breakpoint where you think this conversion is called. \$\endgroup\$ Feb 8 at 16:47
  • \$\begingroup\$ Sorry I've missed a part out again, the conversion works fine, its just I serialize the data to a file and the error occurs in this Line when trying to deserialize the file back into a SaveSlot SaveSlot slotResult = formatter.Deserialize(slotStream) as SaveSlot; the 'SaveSlot' contains a 'SaveData', which contains the 'position' Point variable. \$\endgroup\$
    – Pow
    Feb 8 at 17:00
  • \$\begingroup\$ Bear in mind that you'll get a garbage collector allocation every time that implicit operator is triggered and produces an array. Do you need to convert to an array, or would it suffice to implement the [] operator? (Or rely on implicit conversion to Vector3 to lean on its implementation?) \$\endgroup\$
    – LudoProf
    Feb 8 at 17:26

1 Answer 1

0
\$\begingroup\$

I figured it out, it's kinda stupid.

The reason was because before I created the data type I was using a float[], but when I updated it to Point, I still had the old files with float elements in them. After deleting the cache files and remaking them, the error disappeared.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .