C# variables by value and by reference

If I understand correctly, Whenever I do an assignment of a class it assigns a new reference and whenever I do an assignment on a basic type (int, float etc.) or on a struct, they are passed by value.

First thing I was wondering is how do I know something is a struct rather than class (for example, if I look at the Vector3 definition it gives the definition for unity javascript and not C# and it doesn’t say anywhere that it is a struct).

The second thing I was wondering is how to do the opposite (e.g. pass Transform by value or Vector3 by reference) both in simple assignment (a=b) and when passing as a parameter to a method.

in C# anything is a reference, anything you see is an object

But perhaps you are refering to the ref / out keywords? :slight_smile:

While I won’t deny it’s printed in small font, the second word on the Vector3 page is struct ;).

The header of each such page will tell you whether you’re dealing with a class or a struct.

Assuming you are talking about C# here (I’m not sure about JS in this):
There are two ways to pass value types by reference (or reference types by reference, for that matter) to functions, but these functions must explicitly define the parameters as ‘out’ or ‘ref’ ( out keyword - C# reference | Microsoft Learn and ref keyword - C# reference | Microsoft Learn).

There is also a way to convert value types to reference types by using boxing ( Boxing and Unboxing - C# | Microsoft Learn).

However, converting reference types to value types is not possible:
http://msdn.microsoft.com/en-us/library/3ewxz6et(VS.80).aspx

So I can use out/ref if I want to pass a Vector3 by reference to a function but what can I do in assignment?

First let us say I want to set a target position where I want to go to be the position of some object:

Vector3 TargetPos=targetObj.transform.position;

Is there a way to do this assignment by reference (so TargetPos will always be the position of the object)? I guess I can use (through boxing) something like

Object TargetPos=(object)(targetObj.transform.position);

but that would be very cumbersome…

Second Let us say I want to just save the current state of a tranform.
I would like to do something like

Transform curTransform = transform;

but this would only save the reference. Do I have to save every member by itself (and then go deeper) or is there a way to just save the values (i.e. create a copy)
I cannot use unboxing because tranform is not a value type…

It’s not even cumbersome, it’s impossible. Transform.position is a property (a method disguising as a variable) and returns a copy of what it calculated internally. There is no way to ever reference its contents directly.

If you wish to store transform data, you will need to implement this storage yourself. While straightforward for the transformation data, it also includes the complexity of what to do with the transform children, their associated GameObjects and connected Components.

Just use Object.Instantiate(objecttoclone)

you answer makes no sense, First you say: 'in C# anything is a reference" Then you say “in C# anything is a reference”

So am I to ASSume that if you cannot “see” an object in the scene or game view then it is a value or more rightly passed by value?

You’re really asking him a question about a 9 year old post?