Adjusting localPosition

Hi, I am trying to keep an object within its parents boundaries. So I created this script:

Vector3 Position = transform.localPosition;

if (Position.x > 0.5f)
{Position.x = 0.5f;}
print (Position.x);

In order to prevent the object from moving over the 0.5f treshold. However, the object simply doesn’t respond to this treshold.

What am I doing wrong?

You’re not setting the position of the object with Position.x = 0.5f. That instruction only sets the x value of the Vector3 named “Position”. To set the object’s position you need to do transform.localPosition = Position after your example. Does this make sense?