3D Top Down Movement Error

I started following a tutorial on a top down shooter-ish game. The “Player” script he wanted me to do looked like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    //Variables
    public float movementSpeed;
    public GameObject camera;


    //Methods
    void Update()
    {
        Plane playerPlane = new Plane(Vector3.up, transform.position);
        Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
        float hitDist = 0.0f;

        if(playerPlane.Raycast(ray, out hitDist))
        {
            Vector3 targetPoint = ray.GetPoint(hitDist);
            Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
            targetRotation.x = 0;
            targetRotation.z = 0;
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 7f * Time.deltaTime);
        }
    }
}

I have been trying to notice a difference between the two and could not find one. Here is the tutorial link

And here is what the error said:
NullReferenceException: Object reference not set to an instance of an object
Player.Update () (at Assets/Scripts/Player.cs:16)

You can double click an error message in the console and Unity should highlight the offending code for you.
If your code is the same as in the tutorial (did not check it) you have some references. Maybe on of this is not initialized in the inspector? If so you should drag the right object into the field.
Another potential issue is, that you have defined a public GameObject camera whereas you use UnityEngine.Camera.main.ScreenPointToRay. The static field of the type. Capitalization is important in coding. Camera and camera are two different things. First is the type (class), second is the variable you declared.
I suggest to get some basic programming learned first. Search for “C# yellow book”. This is a free ebook. And you should also learn some basic debugging skills since you will have lots of such errors later, no matter what. Debugging helps you to solve these kind of things. Having to wait a day until someone responds to your post is wasted time.

The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. 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

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.

Just wondering does the double click thing work for visual studio on Mac? as I am trying it yet it won’t work.

ok so I fixed the error, nice. now whenever I load up it just shoots into the sky and im not quite sure why. There aren’t any errors though so yeah…

Sounds like a personal problem!

Launch the game, press pause, then study the scene for whatever the transform is that is supposed to be your muzzle… it’s probably just pointed wrong

Or else your bullet prefab is pointed wrong? Or a scripting error…

I don’t know. It worked for VisualStudio on Windows for me some time ago. And it works for MonoDevelop on Linux for me now. I would never touch a Mac ;).
However, you could check the Editor settings as there are some related to IDE. Maybe it just needs some configuration. And there has been a plugin for Visual Studio some time back to better cooperate with Unity (debugging etc.). Not sure if you need this now. Maybe there is a Mac related subforum where you could ask (I know there is one for the Linux Editor). Or you ask official support.

The computer is pretty dumb. In programming your application does what you tell it to do (via source code). When it does the wrong thing you are telling it to do the wrong thing. Just tell it to do the thing you want. This process is called debugging. And often it is as important as programming. So you need to learn it too. First step is usually to verify your assumptions with debug logs. Is something null? Has it the proper values? Are the results of my calculations the one I would expect?