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 :)
When posting a question regarding a compiler error, it'll make it far easier to help you if give you comment your code with something that says
//<---- error here
Even though you give the line number that is not always obvious where it is at. And if you are feeling daring, you could always double-click the error in Unity and Unity will open $$anonymous$$onoDevelop and drop the cursor onto the line that has the problem. This is the way the rest of us solve these kinds of problems.
Thank you for the help! I will make sure I do that from now on.
The Camera.main property needs the GameObject called "Camera $$anonymous$$ain" in scene because otherwise this will not work.
You can have a public variable of camera type which you can use it as your camera.
I do not think this is the problem as I have renamed my camera in the scene "Camera $$anonymous$$ain" but I still get the error.
Did you attach those two scripts to separate game objects? if so, the you probably have to add
private GameObject gun;
as a variable, and
gun = GameObject.Find("Gun");
hasGun = gun.GetComponent<player_gun_manager_script>(); // just add "gun." in front of the GetComponent.
into the Start Function.
I just called the called the Game Object gun, just rename it when it's called differently ^^
and if both scripts are attached to the player then you can Ignore this ^^
Answer by TBruce · May 23, 2016 at 06:33 PM
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.
Hi, thank you for letting me know what the problem is. However, I cannot figure out why Unity thinks the component is not attached.
This is the inspector view of the player object, and, as you can see, it does have the component player_gun_manager_script attached. Please help me figure out the solution to this. Thank you.
Also, I do not believe I had set hasGun to a value in the inspector.
hasGun is a private field so you can not set it in the inspector. You are showing me the object that the player_gun_manager_script is attached to but I see no player_gun_script. So when you go to get the component like this
hasGun = GetComponent<player_gun_manager_script>();
you will error. Where is the object that has the player_gun_script?
This is the script that defines
public GameObject gun_green;
public float thrust_green = 50;
player_gun_manager_script hasGun;
and calls
hasGun = GetComponent<player_gun_manager_script>();
On the GameObject that has the player_gun_script attached you need to do one of three things
Add the player_gun_manager_script to the same GameObject as the one the player_gun_script is on.
$$anonymous$$ake hasGun a public and set it in the inspector.
Search for the GameObject that has the player_gun_manager_script attached to it in the player_gun_script.
Thank you for helping (my apologies for getting back to you so late)
I did as told and now I receive no error and everything is working! I appreciate the help :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612081336im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 2 Answers
Why is object reference not set to an instance? 1 Answer
“Object reference not set to an instance of an object 1 Answer
Errors appear when i get a bad Raycast hit. Meaning when it hits another object in the scene. 0 Answers
PLEASE HELP!! Object reference not set to an instance of an object at ChangeMasterVolume.Update 1 Answer