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
13
Question by madturtle · Mar 09, 2012 at 07:43 AM · variablereference

How do I know whether variable is pass by value or by reference?

For example transform seems always pass by reference, so we usually use code like this:

 myTr=transform;
 

When the object move, myTr also change.

But in other cases, Vector, for example, seems pass by value.

 pos=transform.position;

The variable pos likely doesn't change as object move. It's a little confused sometimes.

Comment
Add comment · Show 1
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 CHPedersen · Mar 09, 2012 at 08:44 AM 0
Share
  • for asking a question that's very relevant to program$$anonymous$$g in general, and for wording it properly so it'll pop up in searches. :)

3 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

Answer by Eric5h5 · Mar 09, 2012 at 07:51 AM

Structs (Vector2, Vector3, Color, etc.) and primitive types (int, float, byte, etc.) are by value, everything else is by reference. The docs say whether something is a struct.

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 fafase · Mar 09, 2012 at 08:00 AM 0
Share

Considering the program$$anonymous$$g level of JS anc C#, would you actually care about how it is passed since it is done by the compiler? Using C or C++, you would have to look and code manually but as far as I know there is no "manual" pointer * or reference & in C# and JS.

avatar image CHPedersen · Mar 09, 2012 at 08:42 AM 2
Share

You can use pointers in standard C# (through the unsafe keyword, and to value types only), but Unity doesn't allow it, even if the language does.

And how something is passed is incredibly important! If you understand when something is by value and when something is by reference, you also understand when you're modifying the value of something versus when you are merely modifying a COPY of the value of something. There's a world of difference there that any programmer should take the time to study thoroughly!

avatar image fafase · Mar 09, 2012 at 09:00 AM 0
Share

Yep, I have not been studying C# deeply yet. I am aware of the principal of copy/value but thought it would not matter if you could not control it which obviously shows that you can.

avatar image Bunny83 · Mar 09, 2012 at 09:17 AM 1
Share

Actually everything is passed by value except, in case of function parameters, it's a ref or out parameter. If you assign a reference type to another variable, it's value (the reference) gets copied. Value type consists of the data it's made up. When a struct (value type) is copied it's content (the value) is being copied.

UnityScript is a bit confusing because you don't have to use the ref / out keyword on ref or out parameters of functions like Physics.Raycast's hitInfo parameter. It's a struct but an out parameter so it's passed by reference.

avatar image fafase · Mar 09, 2012 at 09:25 AM 0
Share

In the case Unity has no documentation on the subject, wouldn't it be nice if someone who masters the subject could come up with a full explanation (not 100 pages though) posted on the forum so that anytime the question rises in someone's head, he would find the answer. Furthermore, it could be updated with comments of other users. Just an idea. And $$anonymous$$SDN sometimes looks like it has been written by a computer and is not so humanly readable if you don't have a Phd in computer science.

avatar image
1

Answer by ps77 · Mar 09, 2012 at 08:05 AM

In C#, Value Types (struct,enum) are passed by value and Reference Types (class,interface,delegate) are passed by reference. You can pass a Value Type as reference using the ref keyword.

See MSDN Value Types and Reference Types.

Comment
Add comment · Show 1 · 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 Bunny83 · Mar 09, 2012 at 09:34 AM 0
Share

Reference types are not passed by reference. The content of a reference type variable is the reference. You can also explicitly pass a reference type by reference, that's something completely different.

 //C#
 void SomeFunction($$anonymous$$yClass obj)
 {
     obj.someValue = 5;  // use the copied reference to access a member of the referenced object.
     obj = new $$anonymous$$yClass(); // overwrite the reference with a new instance
     obj.someValue = 100000;
 }
 
 $$anonymous$$yClass someObject = new $$anonymous$$yClass();
 SomeFunction(someObject);
 Debug.Log(someObject.someValue); // this will print "5"

In this case the obj parameter got copied. The function has a local copy of the reference. If you change the reference, the passed reference doesn't get changed. The object created in SomeFunction will be lost when the function ends since the reference is only stored locally.

 void SomeFunction(ref $$anonymous$$yClass obj)
 {
     obj.someValue = 5;  // use the copied reference to access a member of the referenced object.
     obj = new $$anonymous$$yClass(); // overwrite the reference with a new instance
     obj.someValue = 100000;
 }
 
 $$anonymous$$yClass someObject = new $$anonymous$$yClass();
 SomeFunction(ref someObject); // In C# you have to use the ref keyword for passing ref parameters.
 Debug.Log(someObject.someValue); // this will print "100000"

In this case the parameter obj is passed by reference. You directly use the someObject variable in the function. It's a reference of a reference type.

It sounds confusing but it makes sense if you get behind it. :D

avatar image
1

Answer by FishBone · Mar 09, 2012 at 08:00 AM

Structs are passed as value and Classes are passed as reference.

If you want to pass a Struct as a reference, you can define the parameter in the function with 'ref' modifier

like for example:

 void DoSomething(ref Vector3 vec)
 {
     vec.y = 0;
 }
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

10 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

Related Questions

Store a GameObject to a variable on runtime. 1 Answer

Converting JS to C# 1 Answer

value of another script not being read? 0 Answers

How to change a value of a reference object variable after a while ? 0 Answers

access a var from another script 3 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