- Home /
Quad Objects not responding to clicks.
I am very new to Unity and C#. I saw this Image Sliding Puzzle tutorial on youtube so I've been working on that. So, far I have instantiated the quad objects and now I want the blocks to respond to clicks. To be precise I want the block that is clicked to be exchanged with the the empty block.
I have two classes puzzle and block.
public class Puzzle : MonoBehaviour {
public int blocksPerLine = 4;
Block emptyBlock;
void Start()
{
CreatePuzzle();
}
void CreatePuzzle()
{
for (int y = 0; y < blocksPerLine; y++)
{
for (int x = 0; x < blocksPerLine; x++)
{
GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
blockObject.transform.position = -Vector2.one * (blocksPerLine - 1) * .5f + new Vector2(x, y);
blockObject.transform.parent = transform;
Block block = blockObject.AddComponent<Block>();
block.OnBlockPressed += PlayerMoveBlockInput;
if (y == 0 && x == (blocksPerLine -1))
{
blockObject.SetActive(false);
emptyBlock = block;
}
}
}
Camera.main.orthographicSize = blocksPerLine * 1.5f;
}
private void PlayerMoveBlockInput(Block blockToMove)
{
Vector2 targetPosition = emptyBlock.transform.position;
emptyBlock.transform.position = blockToMove.transform.position;
blockToMove.transform.position = targetPosition;
}
}
public class Block : MonoBehaviour {
public event System.Action<Block> OnBlockPressed;
private void OnMouseDown()
{
if(OnBlockPressed != null)
{
OnBlockPressed(this);
}
}
}
I even tried writing log statements to see if the click is actually working, But it won't work as well.
Any suggestion would be really helpful :) I am also interested in any ideas to do this in a better and simpler way.
Answer by Piyush_Pandey · Jul 18, 2018 at 11:07 AM
Make sure that on your quad:
1) Block script is attached
2) It is having a collider attached to it.
The script Block will take OnMouseDown trigger only when the collider is attached to the same object. Do not attach it to any child/sibling/parent respective to the collider.
Thank You so much for replying.
Yes I have the block script attached to my quad ( line 20 in the puzzle script does that i guess ) . Even while I click on individual quads it shows the block attached to it. And I don't completely understand the part where you wrote "The script Block will take On$$anonymous$$ouseDown trigger only when the collider is attached to the same object. Do not attach it to any child/sibling/parent respective to the collider." I am really sorry, but I am very new to unity. I still appreciate your help :)
In unity you can create a hierarchy of objects by making a gameobject as child of another. By Do not attach it to any child/sibling/parent respective to the collider. what i mean is:-
1) Take the quad and attach to it the Block script.
2) Add a collider component to the same EXACT gameobject where you added the Block script, not anywhere else.
I would recommend you to go through some Unity video lessons that are available on youtube or Unity's website itself.
Thank You