- Home /
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.
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. :)
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.
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.
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!
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.
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.
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.
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.
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
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;
}
Your answer
Follow this Question
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