When I want to change position of a transform in Unity, I usually do as follows:
var x = 10f;
transform.position = new Vector3(x, transform.position.y, transform.position.z);
But I think this is a little tedious. So I added this extension method to a Vector3 class.
public static class Vector3Extensions
{
public static void SetX(this Vector3 pos, float x)
{
pos = new Vector3(x, pos.y, pos.z);
}
....
When I called that, there is no error but actually the value is not changed. Yeah I know this would be happened because Vector3 is struct. I tried to add a ref keyword to my methods,
public static void SetX(ref this Vector3 pos, float x)
{
pos = new Vector3(x, pos.y, pos.z);
}
but it doesn't work because "A property or indexer may not be passed as an out or ref parameter" error occurs. I want to do just like as:
transform.position.SetX(10f);
Is there any way? thanks.