transform.position question

using UnityEngine;
using System.Collections;

public class SetupObjects : MonoBehaviour {
	GameObject generic;
	// Use this for initialization
	void Start () {
		generic=GameObject.Find("SpritePlane");
		generic.transform.position.x=0;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Why does that code tell me that:
"Cannot modify the return value of ‘UnityEngine.Transform.position’ because it is not a variable.

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.

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)

using UnityEngine;
using System.Collections;

public class SetupObjects : MonoBehaviour {
	GameObject generic;
	// Use this for initialization
	void Start () {
		generic=GameObject.Find("SpritePlane");
		Vector3 tmpVec=generic.transform.position;
		Debug.Log(tmpVec[0]);
		tmpVec[0]=0;
		generic.transform.position=tmpVec;		
	}	
	// Update is called once per frame
	void Update () {
	}
}

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.

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.

It becomes pretty intuitive after awhile :wink: