How to scale object in Runtime, without loosing position

I’m trying change width of wall elements during runtime.

if (Input.GetMouseButtonDown(0)) {
	transform.localScale += new Vector3(1, 0, 0);
	transform.localPosition += new Vector3(-0.5f, 0, 0);
}
if (Input.GetMouseButtonDown(1)) {
	transform.localScale -= new Vector3(1, 0, 0);
	transform.localPosition -= new Vector3(-0.5f, 0, 0);
}

It works fine, when I have no rotation.
Otherwise the length changed correctly, but the origin position moves somehow depending actual rotation…

Any idea how to correct position regarding rotation ??

If you can’t change the imported models pivot, you can still parent the object under an empty gameobject and then offset in a way that the empty acts as the new pivot. You can then rotate the empty pivot object. This is easiest in the editor but you can still build it in code.

Anyway it doesn’t sound like a good solution, whoever made the model should be able to give you a version that has the correct pivot.