|
For example transform seems always pass by reference, so we usually use code like this: When the object move, myTr also change. But in other cases, Vector, for example, seems pass by value. The variable
(comments are locked)
|
|
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. Considering the programming 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.
Mar 09 '12 at 08:00 AM
fafase
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!
Mar 09 '12 at 08:42 AM
CHPedersen
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.
Mar 09 '12 at 09:00 AM
fafase
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.
Mar 09 '12 at 09:17 AM
Bunny83
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 MSDN 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.
Mar 09 '12 at 09:25 AM
fafase
(comments are locked)
|
|
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. 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. 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. In this case the parameter It sounds confusing but it makes sense if you get behind it. :D
Mar 09 '12 at 09:34 AM
Bunny83
(comments are locked)
|
|
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:
(comments are locked)
|

+1 for asking a question that's very relevant to programming in general, and for wording it properly so it'll pop up in searches. :)