Object Reference not set to an instance of an object.

What is wrong with my code, I’m getting the object reference is not set to an instance of an object error

here’s my code:

using UnityEngine;
using System.Collections;

public class AmmoBox : MonoBehaviour {

	GameObject player; 
	private PlayerShooting PlayerShooting; 
	public GameObject GunBarrelEnd; 



	void Awake()
	{
		 
		player = GameObject.FindGameObjectWithTag ("Player");
		GunBarrelEnd = GameObject.Find ("GunBarrelEnd"); 
		PlayerShooting = GunBarrelEnd.GetComponent <PlayerShooting>(); 
		 


	}

	void OnTriggerEnter (Collider other) 
	{
		if (other.gameObject == player) {
			PlayerShooting.ammo += 20; 

		}
	}


}

This is a NullReferenceException. Let’s investigate: You’re not calling anything on “player” anywhere, so that GameObject can’t be the source of the error.

Therefore, the error is one of these:

1:

There is no GameObject in the scene called GunBarrelEnd, so GameObject.Find returns null when trying to find it, and then you can’t call GetComponent to find the PlayerShooting script.

OR 2:

You do find the GunBarrelEnd GameObject, but it does not have a script called PlayerShooting attached to it, so GetComponent returns null when trying to find it, and then the error happens upon accessing its .ammo variable.

Without more information, there is no way for me to be sure.

private PlayerShooting PlayerShootingScript;

try to make it public and drag the object which having script PlayerShooting.cs on it

using UnityEngine;
using System.Collections;

public class AmmoBox : MonoBehaviour {

 GameObject player; 
 public PlayerShooting PlayerShootingScriptObject; 
 public GameObject GunBarrelEnd; 



 void Awake()
 {
      
     player = GameObject.FindGameObjectWithTag ("Player");
     GunBarrelEnd = GameObject.Find ("GunBarrelEnd"); 
      


 }

 void OnTriggerEnter (Collider other) 
 {
     if (other.gameObject == player) {
         PlayerShootingScriptObject.ammo += 20; 

     }
 }

}