# Oh Unity, what a strange decision you have made.

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, in Unity, this is perfectly legal code, no warnings or compiler errors will show up.

(Though this is pseudo as I don't have the code in front of me)


void SomeFunction(Vector3 value0, Vector3 value1, float deltaTime)
{

Vector2 interpValue = Vector3.Lerp(value0, value1, deltaTime);
}


You see, Unity, in it's infinite wisdom, has made it's vector classes auto-convert from Vector2s to Vector3s and back again.  It just sticks a zero in or drops the last value.  Who cares about catching compile time error checking, right?

Do people like that feature?  It drives me batty, a simple mistype can become a mysterious bug, that would normally be caught at compile time.

##### Share on other sites

Its probably a bit of a misstep to allow implicit conversion like that, but I don't know that its terribly egregious either. Particularly in the other direction its common and convenient to convert implicitly, and its possible they viewed consistency of requiring the explicit conversion as more important than preventing this particular pothole -- although I would personally disagree with that since round-tripping down and back again effectively zero's the trailing dimensions. I would probably be tempted to make upwards conversions implicit, but downwards conversions explicit.

##### Share on other sites

It's certainly not the worst Coding Horror I've ever seen, but it's definitely the first Vector math class/structs that I've seen that allow implicit conversion both ways.  It just doesn't seem like much of an advantage is gained over simple type safety.

##### Share on other sites

Ehh, I'm on a fence on this.

On one side its very convenient, but explicit conversions are kinda annoying to write.

Now, if you could do it in a GLSL-like way, ie:

vec2 interpValue = vec2(lerp(val0,val1,delta).xy);

That would be both convenient and explicit enough for me.

##### Share on other sites
I only use implicit conversion operators if I'm the only person who will EVER use the type. Otherwise it's explicit operators or ToX() methods.

Unity could have easily used 'explicit' instead of 'implicit' when defining those operators. Edited by Nypyren

##### Share on other sites

It kinda depends on legacy of the code -- explicit conversion constructors have been around since C++98, but explicit conversion operators are new in C++11. Depending on how they chose to implement them, and their other considerations in doing so, explicit conversions might not have been an option at the time (outside of named conversion functions, like ToX member functions).

Its also one of those things where changing it after the fact would yield a more-restrictive API surface than before. You can make things less-restrictive over time without breaking source compatibility, but you can't be more-restrictive without breaking source all over the place. Yes, it's better to be explicit, but try telling that to the 100s of millions of lines of unity code that exists, and those who have to maintain it.

##### Share on other sites

It kinda depends on legacy of the code -- explicit conversion constructors have been around since C++98, but explicit conversion operators are new in C++11

Unity uses an old version of C#/Mono

At the least, the code should probably look something like:

Vector2 interpValue = Vector3.Lerp(value0, value1, deltaTime).ToVec2();

or

Vector2 interpValue = Vector3.Lerp(value0.ToVec2(), value1.ToVec2(), deltaTime);

or

Vector2 interpValue = Vector2.Lerp(value0, value1, deltaTime); //overload that takes vec3 arguments

Edited by Hodgman

##### Share on other sites
IIRC it was a change they introduced mid 2013, along with other preparatory changes for the eventual Unity 2D environment.

I'm not seeing it specifically in the change logs, but there is a definite cutoff point where in the unity3d forums people stopped complaining about the difficulty of converting the Vector2 mouse coordinates into vector3 objects.

##### Share on other sites
As nice as it may be it introduces bugs that are horrible to track down as well, for example dropping a script containing this on a 2D-only object (e.g. a sprite) does not produce an error but crashes the editor instantly:

void Update() {    rigidbody.AddForce(Vector3.up * 10);}
Took me while to figure out that the reason for the editor to crash as soon as I hit play was that I wrote "rigidbody" instead of "rigidbody2d"...

Edit: Not even a simple warning is emitted.

##### Share on other sites

As nice as it may be it introduces bugs that are horrible to track down as well, for example dropping a script containing this on a 2D-only object (e.g. a sprite) does not produce an error but crashes the editor instantly:

void Update() {    rigidbody.AddForce(Vector3.up * 10);}
Took me while to figure out that the reason for the editor to crash as soon as I hit play was that I wrote "rigidbody" instead of "rigidbody2d"...

And even then it cringe, as it should be:

void Update() {    rigidbody2D.AddForce(Vector2.up * 10);}


Though you can get away with it anyway, since the up vector just happens to be 0,1,0, it still irks.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 22