- Home /
Touch not registering
I am getting this error message each time I touch an object on an ios platform with the remote app.
NullReferenceException: Object reference not set to an instance of an object test-unity-5.Update () (at Assets/my-scripts/test-unity-5.js:14)
I don't know how to solve it, and don't even know why it is occurring. ( I have a general idea, but...)
My code is:
#pragma strict
private var JumpH = 8;
// Jump Hieght 8 units
private var IsFalling = false;
// Is not falling start
var particle : GameObject;
function Update ()
{
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray)) {
//ad effects here
rigidbody.velocity.y = JumpH;
IsFalling = true;
}
}
}
}
I am touching a button I imported that is stacked in the camera, my player's view. I am new to unity, so please don't be spiteful. Any help is appreciated!
Answer by tanoshimi · Jul 11, 2015 at 05:52 PM
There are two object references on line 14. One is Camera.main
, and the other is touch
. The error message is telling you that one of them is null.
Give that you are iterating through Input.touches, which should only give you an array of touches in the current frame, it seems unlikely that touch
is null. My deduction would therefore be that Camera.main
is null. Do you have a camera in your scene that is tagged as "MainCamera"?
I changed it so that my camera is main camera, but that does not change it. Does having the gameobject with the script imported affect it? Also, my button, a gameobject, is stacked in the camera, so it moves with the camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220611230659im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to display a video behind gui on mobile 2 Answers
First Person Controller Mobile does not work? 3 Answers
Does receipt verification require an internet connection? 0 Answers
DOs and DON'Ts in mobile game development 2 Answers
Parent Buttons 0 Answers