-1

I got this code

if (Input.GetMouseButton(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        ......
   }`

it works with other project that I created but when I tried to implement it with my new project it returns a nullReferenceException.

NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/300357e52574df36/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:276)
Ball.Update () (at Assets/Scripts/Ball.js:51)`

error pointing to this part var ray = Camera.main ..... I tried to add light with the scene but still it's not working. Please tell me what will be the solution. by the way the whole code intended to move or dragged the object/ball.

Lazy Ninja
  • 21,131
  • 9
  • 77
  • 99
11111
  • 57
  • 1
  • 5
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – LearnCocos2D Feb 16 '15 at 10:43

1 Answers1

1

Camera.main returns the first enabled Camera in your scene that has the "MainCamera" tag. If Camera.main is null, you probably deleted a camera and forgot to add the tag to the new one, or all the cameras with that tag is disabled.

ddk
  • 1,723
  • 1
  • 14
  • 18