x


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.

more ▼

asked Mar 09 '12 at 07:43 AM

madturtle gravatar image

madturtle
31 4 5 5

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

Mar 09 '12 at 08:44 AM CHPedersen
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Mar 09 '12 at 07:51 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

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)
10|3000 characters needed characters left

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.

more ▼

answered Mar 09 '12 at 08:05 AM

ps77 gravatar image

ps77
16 1 1 2

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(MyClass obj)
{
    obj.someValue = 5;  // use the copied reference to access a member of the referenced object.
    obj = new MyClass(); // overwrite the reference with a new instance
    obj.someValue = 100000;
}

MyClass someObject = new MyClass();
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 MyClass obj)
{
    obj.someValue = 5;  // use the copied reference to access a member of the referenced object.
    obj = new MyClass(); // overwrite the reference with a new instance
    obj.someValue = 100000;
}

MyClass someObject = new MyClass();
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

Mar 09 '12 at 09:34 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Mar 09 '12 at 08:00 AM

FishBone gravatar image

FishBone
151 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x825
x263

asked: Mar 09 '12 at 07:43 AM

Seen: 1599 times

Last Updated: Mar 09 '12 at 09:34 AM