Whats the difference between Vector3.zero and Vector3(0,0,0)?
Ive been making a game in unity for a while but i keep running into a problem with vectors... Why am i not allowed to say Vector3(0,0,0) or Vector3(transform.position.x,transform.position.y,transform.position.z)? I see people using it in tutorials but for some reason i get errors saying it cant convert object expression into UnityEngine.Transform.... i could really use some help! Thanks in advance!
Answer by Julien-Lynge · Mar 01, 2013 at 01:55 AM
You should probably spend some time looking at the script reference for Transforms, to understand what they are. The error is correct - a Vector3 is not a transform, and you can't convert one to the other. You can assign a Vector3 to a transform's variable, like transform.position.
So why would there be an error with transform.position = Vector3(0,0,0); when transform.position = Vector3.zero; doesnt have an error? am i missing something?
Vector3.zero is a object of Vector3. When you write Vector3(0,0,0), you are missing the new
keyword as you are instantiating a new Vector3 object, it should be new Vector3(0,0,0)
.
@Chronos-L is absolutely right. In more specific terms, Vector3.zero is a static property. Vector3(0,0,0) is a constructor. Constructors require the 'new' keyword (to indicate that we're constructing something new).
Do a google search for 'C# static', 'C# property', and 'C# constructor' to learn more.
To show appreciation, you should mark @noaa-julien s answer correct. It is very satisfying for us to see the check mark on our answer as it let us know that we are helping other Unity users out there.
Answer by Owen-Reynolds · Mar 01, 2013 at 06:42 AM
Your tutorials are probably using unityscript (or are just wrong, or both) Unityscript doesn't require as many NEWs in front of everything as C# does, plus other small differences.
Nearly all official Unity3d code examples have a little drop down just above where you can switch to C# (they all start in unity/javascript.)
Answer by harjotmadhopuri · Apr 28, 2021 at 07:15 AM
Vector3.Zero is already saved instance which you can copy to new Vector3 variables by =. The confusion new people gets is that they think Vector3 is reference type but its not. Vector3 is struct and structs are "Value type". So data is copied when you do : Vector3 v = Vector3.Zero. Copying data is faster than creating and assigning data like : Vector3 v = new Vector3(0,0,0).
Read "Reference type " and "Value types " in C# for understanding.
Read the body of the Q -- they're asking why they get errors. It also helps to check the date -- this is 8 years old so they have probably solved it or moved on (but it's fine to answer old Q's if you think others will find it).
A quick note for any new programmers wondering about this speed difference:
@harjotmadhopuri is probably correct that copying a struct in memory is faster than constructing it - it's been so long since I've looked into it that I can't remember. However, I'd caution any new programmers stumbling across this post not to worry about that difference, because any difference would be a couple ten-millionths of a second.
These types of optimizations are algorithm optimizations, meaning you use them when you're going to be creating, say, 10 million Vector3s for some kind of complex calculation. They aren't important for software engineering, where you're more concerned about how readable and understandable your code is when you come back to it in 6 months and have forgotten what you wrote.
So if you're a new programmer, chose between Vector3.zero and new Vector3(0,0,0) based on which makes more sense in your code.