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 /
avatar image
0
Question by brentshermana · Jan 10, 2015 at 05:59 PM · c#rigidbodysyntaxvar

Using var as a placeholder for velocity... why?

I'm working through a few Unity tutorials to get my feet wet with C# scripting, and I came across this:

 var temp = rigidbody2D.velocity;
 temp.y=3;
 rigidbody2D.velocity = temp;

Why is this necessary? I've tried going with rigidbody2D.velocity.y=3; instead, but it won't compile.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Jan 10, 2015 at 06:09 PM

It's because rigidbody2D.velocity is a property, not a direct reference to a Vector3 inside the Rigidbody2d class.

Because Vector3 is a struct, not a class, and therefore a value variable, not a reference variable, the property rigidbody2D.velocity actually gives you a new Vector3 with the same values as the actual Vector3 inside the rigidbody instance. If you'd change the x,y,z values of the Vector3 returned by the property, you would change this new copy of the vector and the actual vector inside the rigidbody would remain unchanged.

More info

Comment
Add comment · Show 3 · 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 brentshermana · Jan 10, 2015 at 06:20 PM 0
Share

If rigidbody2D.velocity only returns the value of the Vector3, why does this line work?

 rigidbody2D.velocity = temp;



avatar image Owen-Reynolds · Jan 10, 2015 at 06:42 PM 0
Share

Read about properties. They are specially written so you can Get the entire value, or Set the entire value, and nothing else. When you use = with a property, it automatically runs the Set function.

This is kinds of an "oops" for C#. $$anonymous$$ost people say "yeah, C# is broken that way, but don't don't make velocity like that. Unless you're making a game or something where speed matters."

avatar image NoseKills · Jan 10, 2015 at 08:02 PM 0
Share

@brentshermana because properties have different logic for "setting" and "getting".

 public SomeStruct velocity
 {
     get { return vector; }
     set { vector = value; }
 }


If you do velocity.y = 2, you are first using the "get" part of the property to get a copy of the vector and then accessing the y component in it.

if you do velocity = Vector3.zero you are assigning directly to the property using the "set" part of it. The value becomes a copy of your vector with your values and that copy is assigned to the internal vector in the struct.

avatar image
0

Answer by Owen-Reynolds · Jan 10, 2015 at 06:32 PM

The reason it has to be done that way is boring technical junk. The important thing is that, yes, it does need to be done that way. Almost anytime you "reach through" an object to a small multipart item, it needs to be pulled-out, changed, then put back.

But that's often what you want to do. Say you wanted to do more with velocity. It's nicer-looking (and not any less efficient) to avoid typing the long name every time:

 Vector3 vel=rigidbody.velocity; // pull out
 if(vel.y>4) vel.y=4;
 if(vel.y<0 && antiGrav==true) vel.y*=0.5f;
 if(vel.magnitude<0.1f) vel=Vector3.Zero; // fake friction
 rigidbody.velocity=vel; // put back

And Unity has some shortcut functions for common changes.

The exact rule is, whenever you have a struct, you can't "reach through" most things. Structs are some built-ins: Vectors (position, velocity,) Color, rotation. Your scripts are classes, so A.getComponent().hitpoints is still fine.

The exact exact reason (I think) is that most members are really getters/setters. dot-velocity is really a C# get. And C# has some funny rules about gets and structs (you can read about them, but it's boring. You can also read about why some people think designing C# that way was just a bad idea.)

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers

Need help with scripting bug fix. pleease 0 Answers

C# RotateAround for 2DRigidBodies 1 Answer

c# syntax ((float>0)?(1):(2)) 2 Answers

Having problems with playing a sound (C#) 2 Answers


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