'Cannot modify .... because it is not a variable' in C#

Hey guys,

I’m pretty new to C# and I’ve decided it’s the language I want to mostly script with in Unity, since I’m fairly familiar with C and C++.

I’ve been going through doing the tutorials and converting the sample scripts to C# as I go, but I’ve come across something that’s got me stumped. In the racing tutorial, the camera script does this inside FixedUpdate():

transform.position.x += 5;

When I try and do the same thing in my C# script, I get the error:

“Cannot modify the return value of ‘UnityEngine.Transform.position’ because it is not a variable.”

and yet this works fine

Vector3 newpos = transform.position;
newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
transform.position = newpos;

Can someone clue me in on what’s going on? A quick google search turned up some stuff about value/reference semantics, but I couldn’t quite get my head around it. Am I right that ‘position’ is a member variable of ‘transform’ that is of type struct? What’s different in the semantics compared to the same thing in C++?

Thanks for the help. I’m loving Unity btw, it’s gotten me back into coding at home and not just at work :slight_smile:

It’s a bad part of the c# design.

Internally, stuff like transform.position is implemented as properties (so when you assign a value to position, a function gets called into the Unity engine). When this is done, you can’t assign into an individual member of the Vector3 struct, but have to assign the entire struct.

in our own implementation of Javascript, we’ve worked around it, but in C#, you need to read the value out into a vector3 and then modify it.

you could also do:

transform.position = transform.position + new Vector3 (0, .5f, 0);

2 Likes

Off topic, sorry.

Out of curiosity, why did you guys make Vector3 a struct, and not a class? Better performance adding Vector3’s to the stack instead of the heap?

Yes. And Vector3 arrays are actually laid out as arrays in memory (as opposed to being arrays of references to Vector3 objects that are lying somewhere else).

hello,
just out of curiosity ( found this thread via google ) :
which way of modifying ‘struct’ property is better for garbage collecting in c# -
either

Vector3 newpos = transform.position; 
newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't? 
transform.position = newpos;

or creating new Vector3 and assigning it to property ?, i.e.

transform.position = transform.position + new Vector3 (0, .5f, 0);

does the Vector3 in the first case get created on stack ? If so, it would be definitely better to use like this, since in second case it gets to be created in heap and garbage collector has to deal with it later - am I right ?
If not, could someone elaborate on this ? :slight_smile:

edit: I mean ti mainly regarding the iPhone development, didnt notice which forum the originating question was :slight_smile:

thanks!

I too just found this via google, so I figured I would answer your question.

Even though Vector3 uses new to instantiate a new vector, it is a struct, not a class, therefore it will follow value type semantics and will be created on the stack.

In other words, even if you create a new vector in this way, it will not create garbage, so neither way should be particularly worse than the other.

Personally I would tend to do:
transform.position += new Vector3 (0.0f, 0.5f, 0.0f);

        Vector3 MyPosition = transform.position;
        MyPosition.x += 5f;
        transform.position = MyPosition;

ok!

1 Like

thanks, lordshitzu ! :smile:

btw a little in-depth explanation of value types - as they should be always considered regardless of being stored on stack - is on Lippert’s blog “Adventure’s in Coding” , e.g. here Fabulous Adventures In Coding | Microsoft Learn
but i guess that in case of more restricted environment such as the iPhone’s the way of handling types must be considered
yet another thing is difference between ms and mono clr implementation (if any)

structs are one of the major reasons IMO that C# has an edge over most other virtual/garbage collection engines. The ability to specify memory to be on the stack and value based is one the key performance benefits of C++. I was very happy to find out that Unity uses this to minimize garbage collection issues that plaque most GC based engines.

To explain it for a C++ developer think of the following c++ scenario:

typedef struct Vector3 {
  int x;
  int y;
  int z;
};

class Transform {
   Vector3 mPos;
public:
   Vector3 position() { return mPos; };
};

In C++ if you did something like:

Transform myTransform;

myTransform.position().x = 5;

it’d actually have no effect on the myTransform class’s member variable because the position() method would return a copy of it’s mPos Vector3 and you’d modify that copy and throw it away all in one line.

In C# they have concept of function calls with variable like access syntax. So in C# when you do

transform.position.x = 5;

you are actually doing in C# exactly what is done in C++ above and C# is smart enough to warn you that you are going to modify something on the stack and throw it away unintentionally.

This exact scenario tripped me up the other day and it took me a good 10 minutes to realize WTH the compiler was telling me. Luckily I read a C# book and played with it for a long time a few years ago so was familiar with this concept and finally remembered it.

1 Like

.position has a get set that both use a Vector3, in javascript you can create variable struct’s like Vector3 without creating a new instance of one, the compiler takes more liberties with what kind of variables you want what you want to use them for.

In C# you get more control over what variables you want to remain null or when you want to initialise them, as a result the compiler will not create a new instance of Vector3 position then pre-populate the Vector3 with the existing get() function (as that would be an additional function call duplication of a Vector3 object that you may not want), so instead you must create your own new instance of a Vector3 populate it with the data you require.

It may seem like C# is being a pain, however the power of a mid-level language like C# allows for much more control over memory management and garbage collection than javascript, I would always use C# in unity for optimisation purposes.

Hope this explains why C# compiles that way =]