Transform Extension Methods

Cannot modify a value type return value of `UnityEngine.Transform.position'. 
Consider storing the value in a temporary variable


This is what you get when you try to directly set a transform x position, like this:

transform.position.x = 0f;

Create a class called ‘TransformExtensions’:

using UnityEngine;

public static class TransformExtensions
    public static void SetX( this Transform transform, float x )
        transform.position = new Vector3(x,transform.position.y, transform.position.z);

And instead of:

transform.position.x = 0f;


transform.SetX( 0f );

One Reply to “Transform Extension Methods”

Leave a Reply

Your email address will not be published. Required fields are marked *