- Home /
Ray error with touches.position
Hi,
I have a little piece of code, to make a raycast on the position you touch the screen (iPhone). I used it in another script, on the exact same way. But now i get an error..
// Buttons
if (Input.touches[i].phase == TouchPhase.Began)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
if (Physics.Raycast(ray, out hit, 100, layermaskButtons))
{
print("test");
}
}
The error is:
NullReferenceExeption: Object reference is not set to an instance of an object
When i dubble click the error, it says that this line is wrong:
Ray ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
But I used that exact same line in another script, and that works perfectly!
What range of values does i take? Is your camera definitely tagged as "$$anonymous$$ain Camera"?
$$anonymous$$y Unity failed or something, I had my camera tagged $$anonymous$$ain Camera, but it just did not get it..
Answer by jgodfrey · Dec 07, 2014 at 03:51 PM
If the line you mention really is the offending line, I'd guess that your scene doesn't have a camera that meets the criteria of the Camera.main member.
Note that according to the docs, Camera.main will return null if the specified criteria is not met.
Jeff
Yes, I know that. But if that was the case, I would not be able to call the exact same code, in another script..
Sorry, this was indeed the problem. Thanks for your help!
Your answer
Follow this Question
Related Questions
Managing Multiple Touch Inputs 0 Answers
Simple (?) touch-command on iPhone 2 Answers
Touching left or right side of iPhone screen? 2 Answers
This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer
ray=Camera.main.ScreenPointToRay(Input.touches[0].position); 1 Answer