Unity Community


Results 1 to 10 of 10

  1. Posts
    1

    '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():

    Code:  
    1. 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

    Code:  
    1. Vector3 newpos = transform.position;
    2. newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
    3. 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

  2. Administrator
    Location
    Copenhagen
    Posts
    1,586
    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);
    Mathematicians that only do calculations that we already know about and are comfortable with? They're called accountants, and they have no friends.


  3. Posts
    890
    Quote Originally Posted by nicholas
    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);
    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?

  4. Unity Graphics Developer

    Location
    Unity Technologies, basement
    Posts
    3,974
    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).


  5. Posts
    81
    hello,
    just out of curiosity ( found this thread via google ) :
    which way of modifying 'struct' property is better for garbage collecting in c# -
    either
    Code:  
    1. Vector3 newpos = transform.position;
    2. newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
    3. transform.position = newpos;
    or creating new Vector3 and assigning it to property ?, i.e.
    Code:  
    1. 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 ? :)

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

    thanks!


  6. Posts
    1
    Quote Originally Posted by rupert618
    hello,
    just out of curiosity ( found this thread via google ) :
    which way of modifying 'struct' property is better for garbage collecting in c# -
    either
    Code:  
    1. Vector3 newpos = transform.position;
    2. newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
    3. transform.position = newpos;
    or creating new Vector3 and assigning it to property ?, i.e.
    Code:  
    1. 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 ?

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

    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);


  7. Location
    Zenith Star
    Posts
    171

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

    Code:  
    1.        Vector3 MyPosition = transform.position;
    2.         MyPosition.x += 5f;
    3.         transform.position = MyPosition;
    ok!
    I love my wife
    very much in love
    -------------------------
    use by: Unity Pro , Asset Server


  8. Posts
    81
    Quote Originally Posted by lordshitzu
    Quote Originally Posted by rupert618
    hello,
    just out of curiosity ( found this thread via google ) :
    which way of modifying 'struct' property is better for garbage collecting in c# -
    either
    Code:  
    1. Vector3 newpos = transform.position;
    2. newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
    3. transform.position = newpos;
    or creating new Vector3 and assigning it to property ?, i.e.
    Code:  
    1. 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 ? :)

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

    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);
    thanks, lordshitzu ! :D

    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 http://blogs.msdn.com/ericlippert/ar...on-detail.aspx
    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)


  9. Location
    Austin, Texas
    Posts
    164
    Quote Originally Posted by rupert618
    Quote Originally Posted by lordshitzu
    Quote Originally Posted by rupert618
    hello,
    just out of curiosity ( found this thread via google ) :
    which way of modifying 'struct' property is better for garbage collecting in c# -
    either
    Code:  
    1. Vector3 newpos = transform.position;
    2. newpos.x += 5.0f; // why does this work while 'transform.position.x += 5.0f;' doesn't?
    3. transform.position = newpos;
    or creating new Vector3 and assigning it to property ?, i.e.
    Code:  
    1. 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 ?

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

    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);
    thanks, lordshitzu !

    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 http://blogs.msdn.com/ericlippert/ar...on-detail.aspx
    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:
    Code:  
    1. typedef struct Vector3 {
    2.   int x;
    3.   int y;
    4.   int z;
    5. };
    6.  
    7. class Transform {
    8.    Vector3 mPos;
    9. public:
    10.    Vector3 position() { return mPos; };
    11. };

    In C++ if you did something like:
    Code:  
    1. Transform myTransform;
    2.  
    3. 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
    Code:  
    1. 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.


  10. Location
    Ireland
    Posts
    2
    .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 =]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •