ScreenPointToRay(Input.mouseposition) throws nullreferenceexeption

Hello, in my code here:

using UnityEngine;
using System.Collections;

public class rotatetomouse : MonoBehaviour {
	public float speed = 10;
	// Update is called once per frame
	void Update () {
		Plane playerPlane = new Plane(Vector3.up, transform.position);
		Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
		float hitdist = 0;
		if(playerPlane.Raycast(ray, out hitdist)){
			Vector3 targetPoint = ray.GetPoint(hitdist);
			Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

line 13: Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); throws nullreferenceexeptions about 1/3 of the time

this is the error

UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:376)
rotatetomouse.Update () (at Assets/scripts/rotatetomouse.cs:13)

i personally have no idea what would be causing this, since it isnt doing it all the time
i have googled around a bit for the problem but everybody else that was getting errors in this kind of statement was replaceing Camera.current.ScreenPointToRay(Input.mousePosition);
and they didnt have a “MainCamera” in their scene

I had the same problem.

give the tag “MainCamera” to your camera!