• 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 pandatree12 · Feb 02, 2017 at 11:42 PM · errorrigidbodycomponentnull reference exceptionobject reference

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

I keep getting 999+ of the error "NullReferebceException: Object reference not set to an instance of an object" The line of code: var rb = object.GetComponent(Rigidbody);

In the script I have it where if you click it instantiates a prefab there, and attached to the prefab are some scripts, colliders, and rigidbodies. After getting its Rigidbody I apply force to it. My script works just as I wanted it to. How can I fix this error, or is there anyway I can just ignore it? I would just disable error popups in the console but I still need to check for other errors. Thanks in advance.

Comment
Add comment · Show 2
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 hexagonius · Feb 03, 2017 at 09:23 AM 0
Share

if you're satisfied with your script just outcomment the line and everything related to rb, because it's null anyway.

avatar image pandatree12 hexagonius · Feb 03, 2017 at 05:27 PM 0
Share

It says it is null but without it the object doesn't move anywhere, I would just eliminate the line of code and to apply the force I would do object.AddForce(0, 0, 1000); or something like that but in the new Unities you have to get the rigidbody before doing that like var rb = object.GetComponent(Rigidbody);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HenryStrattonFW · Feb 04, 2017 at 11:30 PM

Errors like this should never be "ignored" per say, thats a very dangerous approach to take, especially given that unity will stop running scripts that throw them at runtime.

I'd advise just putting null checks around the offending lines, for example.

 var rb = null;
 if( object != null)
 {
     rb = object.GetComponent(Rigidbody);
 }

but keep in mind going down this route would mean that now rb is null, and similar checks would need to be placed around code that uses that.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

error CS1061: Are you missing an assembly reference? 2 Answers

error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it 1 Answer

Why am I getting a null reference error? 1 Answer

Not sure what this error is telling/asking me to do? 1 Answer

Problem Destroying Component 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges