Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by FlyingFlambe · Sep 26, 2016 at 06:18 AM · nullreferenceexceptioninstancenull reference exceptionobject reference

How do I fix a Null Reference Exception (object reference not set to an instance of an object)?

The error continues to show up and I'm not really understanding why. :/

The error appears on the line "if (hit.collider.tag == ..." as well as on the line "hit.collider.gameObject.GetComponent().move = new Vector2(...)". The error seems to occur a lot, and the game still runs (albeit broken) even when the errors appear. What's going on?

Thank you in advance.

     public void HpmStandardUp()
     {
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
         GameObject hexPlateGameObject = GameObject.FindGameObjectWithTag("HexPlate");
 
         if (hit.collider.tag == "HexPlate" && (hit.collider != null))
         {
             hpmTimer += Time.deltaTime;
             CalculateHexPlateAcceleration();
             hexPlateVelocity += hexPlateAcceleration * Time.deltaTime;
 
             HexPlateController hexPlateController = hexPlateGameObject.GetComponent<HexPlateController>();
             hit.collider.gameObject.GetComponent<HexPlateController>().move = new Vector2(0, hexPlateNormalSpeed.y + hexPlateVelocity + hpmStandard + Mathf.Pow(hpmTimer, hpmPower));
             print("Hex Plate Velocity: " + hexPlateVelocity);
         }
         else
         {
             hpmTimer = 0;
             //CalculateHexPlateVelocity();
             //print("Hex Plate Velocity: " + hexPlateVelocity);
             //GetComponent<HexPlateController>().move = new Vector2(0, hexPlateNormalSpeed.y);
         }
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by doublemax · Sep 26, 2016 at 07:26 AM

 if (hit.collider.tag == "HexPlate" && (hit.collider != null))

Change to:

 if ( hit.collider != null && hit.collider.tag == "HexPlate")

You should check the collider for null before you use it.

Comment
Add comment · 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
0

Answer by FlyingFlambe · Sep 26, 2016 at 07:26 PM

Thanks for the solution and reasoning, @doublemax. Still having the issue with the second line though. If it helps, the script listed above is on the player, but the HexPlateController component is on the object being moved.

Comment
Add comment · Show 1 · 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 doublemax · Sep 26, 2016 at 09:27 PM 0
Share

As always, you should program defensively.

 hit.collider.gameObject.GetComponent<HexPlateController>().move

You're assu$$anonymous$$g that "hit.collider.gameObject.GetComponent()" returns something valid and call its method. But what if it returns null?

If a gameobject with tag "HexPlate" should always have such a component, there must be an error somewhere else. Use Debug.Log to print 'hit.collider.gameObject.name' and check if it's the object you're excepting and if it really has a component "HexPlateController".

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

72 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Null Reference Exception anytime scene is reloaded? 1 Answer

Faulty NullReferenceException? 2 Answers

Error That Doesn't Affect My Game Is Spammed Every Frame. 1 Answer

Object reference not set to an instance of an object 1 Answer

Having a issue with a NullReferenceException 1 Answer


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