Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.position question

Discussion in 'Scripting' started by zumwalt, May 5, 2007.

  1. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SetupObjects : MonoBehaviour {
    6.     GameObject generic;
    7.     // Use this for initialization
    8.     void Start () {
    9.         generic=GameObject.Find("SpritePlane");
    10.         generic.transform.position.x=0;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    Why does that code tell me that:
    "Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable.
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    The error message is accurate. Transform position isn't a variable, but a property. You'll need to fetch transform.position into a temporary Vector3, change its x there, and then save it back to transform.position.
     
  3. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Docs are way to cryptic for my tastes sometimes.
    Couldn't find what you suggested anywhere in them.
    Thanks to that suggestion, here was the working results.
    (another reason why I think docs need expanded on)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SetupObjects : MonoBehaviour {
    6.     GameObject generic;
    7.     // Use this for initialization
    8.     void Start () {
    9.         generic=GameObject.Find("SpritePlane");
    10.         Vector3 tmpVec=generic.transform.position;
    11.         Debug.Log(tmpVec[0]);
    12.         tmpVec[0]=0;
    13.         generic.transform.position=tmpVec;     
    14.     }  
    15.     // Update is called once per frame
    16.     void Update () {
    17.     }
    18. }
    19.  
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Your initial approach does work in JavaScript. It does not work in C# though, because structs (and Vector3 is a struct) are always passed and returned by value. So the moment you write "transform.position" in C#, you get just a value back, it already does not know it comes from "transform.position". Hence you need to copy it to temp. variable, change it, and assign it back.
     
  5. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I am finding this to be the case on many levels throughout the docs and the examples, since it was all geared towards javascript to begin with, I am finding it hard to translate to C#. Starting to catch on though. I am trying to avoid the work around which is to switch all that I am doing over to Javascript.
     
  6. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    It becomes pretty intuitive after awhile ;)