- Home /
OnMouseUp is nulling inputs
I am making a simple jigsaw puzzle and I am using OnMouseUp to lock my pieces after you release them. When I release the mouse over the piece it is coming back as null. That is to say, the variable that is being checked if its over a correct piece, is reading null within OnMouseUp but only within OnMouseUp. When the variable was outside it was correct, but the second it is inside OnMouseUp it becomes null.
private Vector3 screenPoint;
private Vector3 offset;
public GameObject link;
public bool selected = false;
public int matchN = 1;
public int matchS = 1;
public int matchE = 1;
public int matchW = 1;
void Start ()
{
}
void Update ()
{
}
void OnTriggerStay2D (Collider2D myCollider)
{
if (myCollider.name == "north" && gameObject.collider2D.name == "south") //checks if correct cube
{
if (gameObject.transform.parent.GetComponent<Tile>().selected == true)
{
matchN = 2;
link = myCollider.transform.parent.gameObject;
Debug.Log (link + " Link");
}
}
if (myCollider.name == "south" && gameObject.collider2D.name == "north")
{
if (gameObject.transform.parent.GetComponent<Tile>().selected == true)
{
matchS = 2;
link = myCollider.transform.parent.gameObject;
}
}
if (myCollider.name == "east" && gameObject.collider2D.name == "west")
{
if (gameObject.transform.parent.GetComponent<Tile>().selected == true)
{
matchE = 2;
link = myCollider.transform.parent.gameObject;
}
}
if (myCollider.name == "west" && gameObject.collider2D.name == "east")
{
if (gameObject.transform.parent.GetComponent<Tile>().selected == true)
{
matchW = 2;
link = myCollider.transform.parent.gameObject;
}
}
}
void OnMouseDown()
{
if (gameObject.name == "piece")
{
//Debug.Log (matchN + "down");
selected = true;
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseUp()
{
matchN = 2;
matchS = 2;
matchE = 2;
matchW = 2;
Debug.Log (matchN + "up");
Debug.Log (link + "up");
selected = false;
if(matchN == 2)
{
Debug.Log("Link north!");
link.transform.parent.transform.position = gameObject.transform.parent.position + new Vector3(0.0f,-1.0f,0.0f);
}
if(matchS == 2)
{
link.transform.parent.position = gameObject.transform.parent.position + new Vector3(0.0f,1.0f,0.0f);
}
if(matchE == 2)
{
link.transform.parent.position = gameObject.transform.parent.position + new Vector3(-1.0f,0.0f,0.0f);
}
if(matchW == 2)
{
link.transform.parent.position = gameObject.transform.parent.position + new Vector3(1.0f,0.0f,0.0f);
}
}
void OnMouseDrag()
{
if (gameObject.name == "piece")
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
}
Comment