Camera shifts when instantiating an object

Hi, I am attempting to instantiate an object 10 units (degrees, feet, I am not sure what the measurement is called) in front of me. I am using this code to do so.

var newObject : Transform;

function Update () {
if (Input.GetButtonDown(“Wall Creation”)) {
transform.position = transform.position + transform.forward * 10;
Instantiate(newObject, transform.position, transform.rotation);
}
}

When I run the script the wall I am trying to create appears where it is supposed to, but my camera changes position. It still follows the player, but it gets moved in a direction (which seems to be pretty random) and will not move back to the character. It took me a while to figure out how to change the position of an instantiated object so any help clearing up this issue would be greatly appreciated.

transform.position = transform.position + transform.forward * 10;

This will move the object that the script is attached to forward 10 units.

You need to save the result of the Instantiate call by casting it to a GameObject, ie:

GameObject wall = (GameObject) Instantiate( … etc

then you would do:
wall.transform.position = transform.position + (transform.forward * 10);

Try this instead

var newObject : Transform;

function Update()
{
    if (Input.GetButtonDown("Wall Creation"))
    {
        Instantiate(newObject, transform.position = transform.position + transform.forward * 10, transform.rotation);
    }
}

Just put it in the instantiate parameters.

What you are doing here, is first moving forward 10 units, then instantiating the wall where you moved to.

Instead, to instantiate the wall 10 meters ahead, you can do this instead:

var newObject : Transform;
    
function Update () { if (Input.GetButtonDown("Wall Creation")) { 
    Instantiate(newObject, transform.position + transform.forward * 10, transform.rotation); 
} 

}