Question by
LightAxe2402 · Jul 16, 2018 at 07:15 AM ·
c#collisionmovement
OnMouseDown Not Working after Moving
Hey, so I am trying to make a simple game where you select two tiles and then the tiles will switch positions. In trying to do this I am using the OnMouseDown() to see when the objects are clicked, however, once I have moved a tile that has a child object it will no longer be "clickable" so it works the first time but then after moving it stops firing completely.
The code is as follows.....
public class Tile : MonoBehaviour {
//Public Variables
public GameManager manager;
public int nameIndex;
[Range(0f, 1f)]
public float scaleFactor = 0.25f;
//Editor Functions
#region
//Shape Functions
public void SetSquareShape()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
GameObject.DestroyImmediate(currentChild);
}
GameObject squareChild = Instantiate(manager.square);
squareChild.transform.SetParent(gameObject.transform);
squareChild.name = "square_shape " + nameIndex.ToString();
squareChild.transform.localScale = new Vector3(transform.localScale.x * scaleFactor, transform.localScale.y * scaleFactor, 1f);
squareChild.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
public void SetCircleShape()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
GameObject.DestroyImmediate(currentChild);
}
GameObject circleChild = Instantiate(manager.circle);
circleChild.transform.SetParent(gameObject.transform);
circleChild.name = "circle_shape " + nameIndex.ToString();
circleChild.transform.localScale = new Vector3(transform.localScale.x * scaleFactor, transform.localScale.y * scaleFactor, 1f);
circleChild.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
public void SetTriangleShape()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
GameObject.DestroyImmediate(currentChild);
}
GameObject triangleChild = Instantiate(manager.triangle);
triangleChild.transform.SetParent(gameObject.transform);
triangleChild.name = "triangle_shape " + nameIndex.ToString();
triangleChild.transform.localScale = new Vector3(transform.localScale.x * scaleFactor, transform.localScale.y * scaleFactor, 1f);
triangleChild.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
public void SetDiamondShape()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
GameObject.DestroyImmediate(currentChild);
}
GameObject diamondChild = Instantiate(manager.diamond);
diamondChild.transform.SetParent(gameObject.transform);
diamondChild.name = "diamond_shape " + nameIndex.ToString();
diamondChild.transform.localScale = new Vector3(transform.localScale.x * scaleFactor, transform.localScale.y * scaleFactor, 1f);
diamondChild.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
//Color Functions
public void SetRedColor()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
currentChild.GetComponent<SpriteRenderer>().color = manager.red;
}
}
public void SetBlueColor()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
currentChild.GetComponent<SpriteRenderer>().color = manager.blue;
}
}
public void SetGreenColor()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
currentChild.GetComponent<SpriteRenderer>().color = manager.green;
}
}
public void SetYellowColor()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
currentChild.GetComponent<SpriteRenderer>().color = manager.yellow;
}
}
//Reset Function
public void Reset()
{
if (transform.childCount > 0)
{
GameObject currentChild = transform.GetChild(0).gameObject;
GameObject.DestroyImmediate(currentChild);
}
}
#endregion
public void OnMouseDown()
{
Debug.Log("Clicking");
if (manager.selectedTile == null)
{
manager.selectedTile = this.gameObject;
gameObject.GetComponent<SpriteRenderer>().color = manager.selected;
}
else
{
if (manager.selectedTile != this.gameObject)
{
Debug.Log("Moving the Icons");
//Switching the Tiles
Vector3 otherPos = manager.selectedTile.transform.position;
Vector3 myPos = transform.position;
manager.selectedTile.transform.position = myPos;
gameObject.transform.position = otherPos;
manager.selectedTile.GetComponent<SpriteRenderer>().color = manager.not_selected;
manager.selectedTile = null;
}
else {
manager.selectedTile.GetComponent<SpriteRenderer>().color = manager.not_selected;
manager.selectedTile = null;
}
}
return;
}
}
As said its just the tiles with the children shapes that arent working after being moved the blank tiles work fine.
Any help appreciated, thanks!
Comment