• 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 /
avatar image
0
Question by Woodpig · Jul 02, 2017 at 08:46 PM · respawnenablerespawningenable and disable script

(Solved!) OnEnable spams error messages

I'm using Void OnEnable () to reset some values when respawning enemies. It works perfectly except for the error messages that keeps spamming my console.

Here's an example:

 void OnEnable ()
     {
             spotted = false;
             activated = false;
             isGrounded = false;
             myRigidbody.gravityScale = 0f;
 
             anim.SetBool ("Activated", false);
     }

As soon as I press play, I get a bunch of error messages stating the following:

"NullReferenceException: Object reference not set to an instant of an object"

I need to tell my enemy that he hasn't already seen the player (when respawned) etc. So, how do I get around this? Would be very grateful for an answer.

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

1 Reply

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

Answer by ShadyProductions · Jul 02, 2017 at 08:55 PM

Either myRigidbody is null and does not exist or anim is null and does not exist.

You can fix this by adding checks:

 if (myRigidbody != null)
              myRigidbody.gravityScale = 0f;
 if (anim != null)
              anim.SetBool ("Activated", false);
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 Woodpig · Jul 02, 2017 at 09:02 PM 1
Share

Wow! That was it. Thank you so much! <3

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

65 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

Related Questions

how can i make it so my blocks switch from grass to log because i have a switch script but i cant make the 2 blocks turn on and off for placing please help 0 Answers

Script isn't disabling? 1 Answer

How to create a duplicate of an object after a specific time with the same starting properties? 1 Answer

Script has been enabled during the gameplay, but never called. 0 Answers

Wave manager that enables multiple gameobjects after X seconds? 0 Answers

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