I have custom buttons created by sprites, which has component of BoxCollider2D. I want to Raycast for the detection of touch. following is what I set it up: There are 3 buttons in a Control_3, BtnLeft, BtnCenter and BtnRight, those setting: And following is the code what I did fot the detection of touch: void update(){ int i =0; while(i <Input.touchCount){ Touch t =Input.GetTouch(i); if(t.phase ==TouchPhase.Began){ RaycastHit2D hit; hit =Physics2D.Raycast(Camera.main.ScreenToWorldPoint(t.position),Vector2.zero); Debug.Log(hit.collider); if(hit.collider !=null){ Debug.Log(hit.collider.name); } } i++; } } However, the hit.collider always return a null value. I have no idea why would it happen. Could anyone help me, please?
collider is the property for the 3d version so it will be null if you have a 2d collider on it. I think there is a collider2d property.