How to make compiler fails when passing a Vector3 value to a Vector2 variable?

Here is the situation

Vector2 v = new Vector3();

that code compiles, there is a way to prevent this?

If that code compiles, I think it’s probably because of the Unity team overloading an implicit casting operator. This means that they have a defined a way to cast from one type to the other (which I suspect just takes the x and y values), without having to explicitly cast. This code isn’t putting a Vector3 in place of a Vector2, what it’s actually doing is equivalent to this: Vector2 v = (Vector2)(new Vector3());. It creates a Vector3, then casts it to a Vector2, implicitly.

As for how to prevent this, the short answer is that you can’t. You could create your own Vector3 structure, in which you can define an explicit cast to Unity’s Vector3 (but not implicit, as that would allow for the casts to be chained, i.e. (Vector2)(Vector3)(new YourVector3()); to be done implicitly by the compiler, while an explicit cast would force you to at least type the cast to Unity’s Vector3), but short of that, there’s nothing you can really do about it.