Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved]Why "transform.position.x" cannot be modifed but "anyVector3.x" can, if both are structs ???

Discussion in 'Scripting' started by jazzbach, May 31, 2018.

  1. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hello.

    Why:
    • "anyGameObject.transform.position.x" cannot be modified
    But:
    • "anyVector3.x" can
    If both are structs and both are of type "Vector3" ?? What's special about "transform.position" ??
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Because 'transform.position' is a property, not a field, and therefore returns a copy of the struct and not a direct reference to the struct in the transform.

    You need to copy out the position, modify, then copy back in.

    Code (csharp):
    1.  
    2. var v = anyGameObject.transform.position;
    3. v.x = 5f;
    4. anyGameObject.transform.position = v;
    5.  
     
  3. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Thank you for your answer lordofduct.

    So, it's like "transform.position" is immutable whereas "anyVector3" is not ??
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I can see how you'd think of it that way.

    Like the example given, when you access the position as a copy, modifying the 'x' (or any) component wouldn't do you any good. Also, like in the example posted, once you've made the copy you can modify it just like anyVector3. The last bit is just re-assigning it to the property.

    Got a little carried away repeating things there already posted, sorry. :)
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    I guess you could think of it that way.

    Really though, you're mutating the vector. It's just that the vector is a different vector copied from the original.

    This is actually one of those things about 'C# properties' that people are super critical of when Microsoft added them to C#. Because a property acts like a function/method, but looks like a field.

    But yeah... when you say "transform.position" think of it more like you're saying "transform.getPosition()".
     
  6. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Don't worry methos5k !! On the absolute contrary. I'm truly thankful to you for further explaining this.
     
  7. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Thanks a lot for your time and explanation lordofduct.

    A little bit off the main topic: I have a background in Java and there are times when I find the "c# property acting like a method but looking like a field" issue a bit disorienting indeed. Times where I don't know if I'm accessing the field directly or it's "accessor". Not a rant at all, just adjusting myself to the c# rules.