Unityscript Problems with translation

I have a script that detects whether an object is on the right of the screen or the left of the screen. If it is on the right it will translate on the x axis left, if it’s on the left, it will translate to the right. I have got this to work on the y axis for a previous object, but now they either go up or down on the y axis even though I have it set to translate on the x axis. Here is the script that I am working on.

#pragma strict

var speed: int = 5;
var isLeft: boolean = false;
var isRight: boolean = false;

function Start () 
{

}

function Update () 
{	
	if(transform.position.x >= 11)
	{
		isRight = true;
		isLeft = false;
	}
	
	if(transform.position.x <= -11)
	{
		isLeft = true;
		isRight = false;
	}
	
	if(isRight == true)
	{
		transform.Translate(Vector3(-speed, 0, 0) * Time.deltaTime);
	}
	
	if(isLeft == true)
	{
		transform.Translate(Vector3(speed, 0, 0) * Time.deltaTime);
	}
}

You don’t need the isLeft or isRight variables; you could just put the Translate code in the appropriate if statements to begin with. However it’s generally better to use Mathf.Clamp for this:

function Update () {
	transform.position.x = Mathf.Clamp (transform.position.x, -11, 11);
}

–Eric

I got it, very weird though. Seems that the x and y switched or something? Not sure

function Update () 
{	
	if(transform.position.x >= 17)
	{
		isRight = true;
		isLeft = false;
	}
	
	if(transform.position.x <= -17)
	{
		isLeft = true;
		isRight = false;
	}
	
	if(isRight == true)
	{
		transform.Translate(Vector2(0, speed) * Time.deltaTime);
	}
	
	if(isLeft == true)
	{
		transform.Translate(Vector2(0, -speed) * Time.deltaTime);
	}
}

You should really use Clamp instead of that method, which will have problems handling framerate fluctuations.

–Eric