Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by romiader · Mar 01, 2013 at 01:41 AM · transformvector3

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image romiader · Mar 01, 2013 at 02:28 AM 0
Share

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?

avatar image Chronos-L · Mar 01, 2013 at 02:49 AM 1
Share

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).

avatar image Julien-Lynge · Mar 01, 2013 at 02:53 AM 0
Share

@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.

avatar image romiader · Mar 02, 2013 at 02:08 AM 0
Share

ok that cleared it up for me thanks!

avatar image Chronos-L · Mar 02, 2013 at 03:32 AM 0
Share

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.

avatar image
1

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Apr 28, 2021 at 01:09 PM 0
Share

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).

avatar image Julien-Lynge · Apr 28, 2021 at 07:10 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I specify the origin and end points of the arc1 instantiated line? 0 Answers

Terrain height -> loosing Players GO childs 0 Answers

Problem with aiming Script(Only Some Code Works) 0 Answers

How can I deal with eulers 1 Answer

Set new default position each time? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges