Null Reference Error

I have a topdown 3d game and im trying to make it so that when the player clicks on “myTree”, myTree will dissappear and “WoodPile01” will appear on the ground. I keep am getting this error and not sure what it means or what to do to fix it.

NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/f724c1acfee760b6/Runtime/ExportGenerated/Editor/Graphics.cs:674)
WoodChop.Update () (at Assets/Scripts/WoodChop.js:5)

var WoodPile01 : GameObject;
var hit : RaycastHit;
function Update() {
	if (Input.GetButtonDown("Fire1")){
		var ray : Ray = Camera.current.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit)){
			if (hit.collider.gameObject.tag =="myTree"){
				Instantiate (WoodPile01,hit.collider.transform.position,hit.collider.transform.rotation) ;
				Destroy(hit.collider.gameObject);
				print ("You Chopped Wood");
			}
		}
	}
}

Replace “Camera.current” with “Camera.main”. From documentation:

Camera.current:

The camera we are currently rendering with, for low-level render control only (Read Only).

Most of the time you will want to use Camera.main instead.

oh forgot to update that. I did change it to Camera.main and there was still no change

It works for me, have your trees been tagged “myTree”?, Have you attached this script to any gameObject in the scene, Is this a full script or only the part of it?

thats what i dont understand it worked for my FPS but its not working for this topdown. trees are tagged “myTree”. this script is attatched to the player gameobject. this is the full script.

ah nvm i fixed it my “camera” wasnt tagged “MainCamera” appreciate the help.

I’ve made a simple scene with this script, attached. You may have to retag all the cubes as “myTree”(cubes are placeholders for trees, you have to retag because because tags can be messed up in unitypackage), and see if it works for you.

505473–17947–$WoodChopping.unitypackage (5.86 KB)

The third alternative to getting the camera because sometimes Camera.current doesnt work, is to use Camera.allCameras[0]