- Home /
RaycastHit2D.collider always null?
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?
Is the hit itself not null? Have you checked? $$anonymous$$ay be the issue is there and not in the collider portion.
It just a typing error The function in my script is upper case. And i can get the debug log message when i touch. However,null vale is as output of the message.
The code you have here assumes an Orthographic camera. Is you camera Orthographic?
Your answer
![](https://koobas.hobune.stream/wayback/20220613163318im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is RaycastHit2d Not working 0 Answers
Problem with custom raycast2D collision detection system. 1 Answer
2D Raycast not working 1 Answer