Add to transform.position.x

I do hope I am not being an idiot here…

I am trying to get the transform.position.x and the transform.position.z of an object, add an amount to it and then crate a new Vector3 with the new x,z coords without affecting the y. Basically so I can instantiate an object just behind it, kind of like this…

var xPos : float = transform.position.x + (size*pieces);

But I get this error:
Operator ‘+’ cannot be used with a left hand side of type ‘float’ and a right hand side of type ‘UnityEngine.Vector3’.

I do understand the error but I have been going around in circles trying to find a solution and I can’t. It really doesn’t seem like it should be so difficult.

Help much appreciated

Assuming size and pieces are both float? You can try breaking it into two lines and see if it helps:

var xPos : float = transform.position.x;
xPos += (size * pieces);

If pieces is a vector, you can do something like this:

  var temp: Vector3 = size * pieces; // calculate the vector offset
  temp.y = 0; // ensure y will not be modified
  var newPos: Vector3 = transform.position + temp; // calculate the new position

or like this:

  var newPos: Vector3 = transform.position;
  newPos.x += size * pieces.x; // add offset x
  newPos.z += size * pieces.z; // add offset z

As the error says, you can’t add a Vector3 to a float. Either “size” or “pieces” is a Vector3. Yes, it’s that simple.