- Home /
How to touch a specific cube
Hello ppl,
I have a group of 4 cubes, the group is named Board and the cubes are childs of the board. I try to raycast to select a cube but i dont know if the problem is the child, but when i make a debug.log i see 4 selections in console, but i want only the cube i selected.
The code i have is this:
void TapSelect(){
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit ;
if (Physics.Raycast (ray, out hit)) {
hit.transform.SendMessage("Selected");
}
}
}
}
Anyone can help?
Thanks in advance.
Answer by DannyB · Jul 16, 2012 at 02:44 PM
This script that you are showing - is it attached to all cubes or to the board?
My guess is that you are getting four correct hits, and you are getting them four times because the script is running four times.
You should attach it to one game object only (the board or even the camera for example) and call TapSelect() on FixedUpdate().
To be sure, do Debug.Log( this.name ) in your void Selected() method.
Thanks a lot Danny, you are perfectly correct. I create another script and attach this to the board, and everything works.
Thanks one more time.