Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
3
Question by aurreco · May 23, 2016 at 04:20 AM · objectobject referenceobject-reference-error

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 :)

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBruce · May 23, 2016 at 05:20 AM 2
Share

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.

avatar image aurreco TBruce · May 23, 2016 at 05:48 PM 0
Share

Thank you for the help! I will make sure I do that from now on.

avatar image rober-psious · May 23, 2016 at 10:53 AM 0
Share

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.

avatar image aurreco rober-psious · May 23, 2016 at 05:50 PM 0
Share

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.

avatar image Fujitaka · May 24, 2016 at 07:45 AM 0
Share

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 ^^

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aurreco · May 23, 2016 at 07:00 PM 0
Share

Hi, thank you for letting me know what the problem is. However, I cannot figure out why Unity thinks the component is not attached.alt text

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.

inspector-view.png (100.5 kB)
avatar image TBruce aurreco · May 24, 2016 at 02:00 AM 0
Share

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

  1. Add the player_gun_manager_script to the same GameObject as the one the player_gun_script is on.

  2. $$anonymous$$ake hasGun a public and set it in the inspector.

  3. Search for the GameObject that has the player_gun_manager_script attached to it in the player_gun_script.

avatar image aurreco TBruce · May 26, 2016 at 01:52 PM 0
Share

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 :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

“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

[Begginer] [Solved] How to solve a Null Reference Exception 1 Answer

Error Object reference not set to an instance of an object!!! Help, I am new! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges