Camera movement problem

I am making code for camera movement but I getting this error.
System.NullReferenceException: Object reference not set to an instance of an object
at MouseHandler.Update () [0x0005c] in C:\Users\mms27\A Test Thing\Assets\FirstPersonMovementDemo\Scripts\MouseHandler.cs:27
the code is here in this code tag.

cam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);

Your ‘cam’ variable has not been assigned properly.

how? I am new to this.

If you’re following a tutorial, back up because you skipped a critical part. You cannot skip ANY parts in a tutorial generally speaking.

If you’re making this up on your own (or the tutorial fails to mention it), you need to put a Camera reference into the cam variable.

You do this by making a public Camera field, like every other editor-time setup step in Unity.

Alternately you can do this using Camera.main, or other ways. Best to stick with the public Camera way because it is The Best Way™.

Also, for future reference, a Null Reference error does not require a forum post. Why?

Because the answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.