How to Lerp a position along one axis only?

I am trying to lerp the local z axis position of a character to the local z axis position of a trigger being entered. I am using Mathf.Lerp but it is triggering an error:

void OnTriggerStay(Collider other) {
	other.transform.localPosition.z = Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1);
}

How could I make this work, or is there a better way?

It gives error, because on C# you can’t access only one axis.

Create a temrorary variable and lerp it, then assign it to the local position.

void OnTriggerStay(Collider other) 
{
     Vector3 newPosition = other.transform.localPosition;
     newPosition.z = Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1);

     other.transform.localPosition = newPosition;
 }

Haven’t tested it, but i’m pretty much creating new Vector3 and assigning it to the local position (So X and Y) can get from other object. Then i Lerp the Z. Lastly i apply the new vector to the other’s local position

you can’t set Z value directly. You have to use its positions.

	void OnTriggerStay(Collider other) {
		other.transform.localPosition = new Vector3(other.transform.localPosition.x, other.transform.localPosition.y, Mathf.Lerp(other.transform.localPosition.z, transform.localPosition.z, Time.deltaTime * 1));

	}