- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
omiyee · Aug 20, 2014 at 02:30 PM ·
javascriptclick
How to detect which block is pressed using javascript
Hi I am trying to detect which cube is pressed but the code. I just have 3 simple cubes which act as a button I want to detect which is pressed at what. i am using it does not work properly when ever i click it clicks all cubes using Javascript
#pragma strict
function Start () {
}
function Update () {
if(Input.GetMouseButtonDown(0))
{
var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
if(hit.transform.gameObject.tag.Equals("Box 3")){
Debug.Log("hit 1");
}
if(hit.transform.gameObject.tag.Equals("Box 2")){
Debug.Log("hit 2");
}
if(hit.transform.gameObject.tag.Equals("Box 1")){
Debug.Log("hit 3");
}
}
}
Comment
Just in case, do you actually have each box tagged and are not just going after the name of the object?
tags.png
(18.3 kB)
Answer by Cherno · Aug 20, 2014 at 02:55 PM
Use a Debug.Drawray to check if you are actually shooting the ray where you want to, and also use CompareTag instead of Equals. check the cube colliders and make sure they don't overlap.