having an issue with the boolean

Not sure why this isn’t working but it will only run the forward function, then it won’t acknowledge a click on going back. I have a feeling its got to do with function OnMouseDown(). Can’t determine what the issue is though.


var cam : GameObject;
var hasMovedLeft : boolean;

function Start()
{
	cam = GameObject.Find("Main Camera");
}

function OnMouseDown() 
{	
	if (gameObject.tag == "up" && hasMovedLeft == false)
	{	
		MoveForwardLeft();
		hasMovedLeft = true;		
	}
	if (gameObject.tag == "down" && hasMovedLeft == true)
	{	
		MoveBackLeft();	
		hasMovedLeft = false;
	}
	
	
}

function MoveForwardLeft()
{
	Debug.Log("REACHING UP STATEMENT");
	cam.animation["cameraLeft"].speed = 1.0;
	cam.animation.Play("cameraLeft");
}

function MoveBackLeft()
{
	Debug.Log("REACHING DOWN STATEMENT");
	cam.animation["cameraLeft"].speed = -1.0;
	cam.animation["cameraLeft"].time = cam.animation["cameraLeft"].length;
	cam.animation.Play("cameraLeft");
}

Are you changing the tag of your gameObject somewhere else in another method? If not, you need to change the tag from “up” to “down” at some point if you want the second if block in your OnMouseDown to work.

They are two separate guiTextures, the script is attached to each

Okay so I found my own answer, I just created a boolean for both, if anyone was curious code is below. I took a walk to think about it.


var cam : GameObject;
static var movedForwardTop : boolean;
static var movedBackwardBot : boolean;

function Start()
{
	cam = GameObject.Find("Main Camera");
	movedForwardTop = true;
	movedBackwardBot = false;
}

function OnMouseDown() 
{	
	if (gameObject.tag == "down" && movedBackwardBot == true)
	{	
		MoveBackLeft();	
	}
	if (gameObject.tag == "up" && movedForwardTop == true)
	{	
		MoveForwardLeft();	
	}	
}

function MoveForwardLeft()
{
	Debug.Log("REACHING UP STATEMENT");
	cam.animation["cameraLeft"].speed = 1.0;
	cam.animation.Play("cameraLeft");	
	movedForwardTop = false;
	movedBackwardBot = true;
}

function MoveBackLeft()
{
	Debug.Log("REACHING DOWN STATEMENT");
	cam.animation["cameraLeft"].speed = -1.0;
	cam.animation["cameraLeft"].time = cam.animation["cameraLeft"].length;
	cam.animation.Play("cameraLeft");
	movedForwardTop = true;
	movedBackwardBot = false;
}