float screenBoundary;
// you need to store position on a local variable
var position = transform.position;
// then you can modify its fields
if(position.x < - screenBoundary)
position.x = -screenBoundary;
if(position.x > screenBoundary)
position.x = screenBoundary;
// and then deploy to the original variable
transform.position = position;
Fun fact:
your last code would actually work, but you was missing some .x and parentheses
if ((transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z)).x < -screenBoundary)
{
transform.position = new Vector3(-screenBoundary, transform.position.y, transform.position.z);
}
if ((transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z)).x > screenBoundary)
{
transform.position = new Vector3(screenBoundary, transform.position.y, transform.position.z);
}
anyway those attributions inside the if conditions aren’t necessaries so you can simplify your code to look like this:
if (transform.position.x < -screenBoundary)
{
transform.position = new Vector3(-screenBoundary, transform.position.y, transform.position.z);
}
if (transform.position.x > screenBoundary)
{
transform.position = new Vector3(screenBoundary, transform.position.y, transform.position.z);
}