Object reference not set to instance of an object?

Hello, I am attempting to make it so that once a trigger is entered, a Boolean value would be set to true. After that, an if statement in another script should allow me to shoot green balls. Here is the code:

The Trigger Script:
using UnityEngine;
using System.Collections;

public class player_gun_manager_script : MonoBehaviour {

    public bool green = false;

    void OnTriggerEnter(Collider other) {
        if (other.gameObject.name == "gun_model_green")
        {
            green = true;
        }
        else
        {
            green = false;
        }
    }

}

The Shooting Script:
using UnityEngine;
using System.Collections;

public class player_gun_script : MonoBehaviour {

    public GameObject gun_green;
    public float thrust_green = 50;
    player_gun_manager_script hasGun;

	void Start () {
        hasGun = GetComponent<player_gun_manager_script>();
	}


	void Update () {

        if (hasGun.green == true && Input.GetMouseButtonDown(0)) {// <------Error here
            GameObject green = (GameObject)Instantiate(gun_green, Camera.main.transform.position + Camera.main.transform.forward, Quaternion.identity);
            green.GetComponent<Rigidbody>().AddForce(Camera.main.transform.forward * thrust_green, ForceMode.Impulse); 
            
        }
	
	}

}

All looks good except the following error:

NullReferenceException: Object reference not set to an instance of an object
player_gun_script.Update () (at Assets/scripts/player_gun_script.cs:17)

The problem seems to be on line 17 of the gun script, but I can’t make out what the issue is… please help. Thank You :slight_smile:

The problem is you are missing a player_gun_manager_script component. Below fixes your error and prints a debug warning

public class player_gun_script : MonoBehaviour
{
    public GameObject gun_green;
    public float thrust_green = 50;
    player_gun_manager_script hasGun;

    void Start ()
    {
        // if hasGun has not yet been set in the inspector see if there is one attched to the current component
        if ((hasGun == null) && (GetComponent<player_gun_manager_script>() != null))
        {
            hasGun = GetComponent<player_gun_manager_script>();
        }
        else
        {
            Debug.LogWarning("Missing player_gun_manager_script component. Please add one");
        }
    }


    void Update ()
    {
        if ((hasGun != null) && (hasGun.green == true) && (Input.GetMouseButtonDown(0)))
        {
            GameObject green = (GameObject)Instantiate(gun_green, Camera.main.transform.position + Camera.main.transform.forward, Quaternion.identity);
            green.GetComponent<Rigidbody>().AddForce(Camera.main.transform.forward * thrust_green, ForceMode.Impulse); 
        }
    }

}

Note: Your code was doing this

hasGun = GetComponent<player_gun_manager_script>();

If you had already set hasGun in the inspector you would be nullifying the value with the statement above.