delegate for object attributes?


I would like to change the x position of an Object but I don’t want to use transform.position.x = something directly.

Is it possible to assign transform.position.x to a variable and then simple use the variable = something instead? This would save me a ton of extra codework.

I would prefer a javascript solution but c# would be ok too.

Here is a very simple example to show what I wan’t to do

var objectXPos :someSpecialType;
if ( localSpace )
	objectXPos = object.transform.localPosition.x;
	objectXPos = object.transform.position.x;    	
objectXPos = 10;

Thanks in advance

‘x’ is component of Vector3

Vector3 is a struct, not a class

so, you could not do it.

but, you can do some class like this:

public class XPositionSetter
    private Transform ObjToSetX;
    public XPositionSetter(Transform t)
        ObjToSetX = t;
    public SetXPosition(float xPos, bool isLocal)
        if (isLocal)
            var tmp = ObjToSetX.localPosition;
            tmp.x = xPos;
            ObjToSetX.localPosition = tmp;
            var tmp = ObjToSetX.position;
            tmp.x = xPos;
            ObjToSetX.position = tmp;

and use XPositionSetter as class and reference type

What about using properties ? Although, this is a C# only solution.

Property definition:

public float xPos {

  get {

    return transform.position.x;


  set {

    // storing position in a temporary variable prevent CS1612 compiler error
    Vector3 formerPosition = transform.position;
    transform.position = new Vector3(value, formerPosition.y, formerPosition.z);




xPos = 4.5f;


(4.5, 67, 89)