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 OliverAnthony · Mar 07, 2016 at 08:34 PM · c#bugupdatemonobehaviournull reference exception

MonoBehaviour Update called twice, fields being set to null

I was chasing down a null reference exception when I came across something a tad disconcerting.

The (apparently null) field is assigned to in only one placed in my entire code, and before that assignment, I check if the value is null (it never is). And yet, when I try and access it outside of the block in which it gets assigned, I get a null reference exception.

This field is defined in class A, which extends MonoBehaviour.

I have a single instance in my scene of a script B, which extends class A. In an attempt to track down when and how the field is becomes null, I dropped a couple of Debug.Log calls into class B's Update method. Class A does not define an Update method. Using VisualStudio I have double-checked that nowhere else in my code is class B's Update method referenced.

Here is the method...

 void Update()
     {
         if ( target != null )
             Debug.Log( "Good reference " + Time.time );
         else
             Debug.Log( "Bad reference " + Time.time );
     }

Somehow, this Update method is being called exactly twice, every frame. Every frame, two lines are output to the console, the first time the method is called, it outputs "Good reference", the second time, "Bad reference."

See the attached imgur link for a sample of my console readout.

http://i.imgur.com/OIBW3n9.png

The stack trace for each Console line looks exactly the same...

 Good reference [current time]
 UnityEngine.Debug:Log(Object)
 MonoToggle:Update() (at Assets/Control/MonoToggle.cs:28)

...for the first line of every frame, followed by...

 Bad reference [current time]
 UnityEngine.Debug:Log(Object)
 MonoToggle:Update() (at Assets/Control/MonoToggle.cs:30)

...for the second line of every frame.

A quick Google search turned up nothing. As far as I can tell no one's ever had this problem.

Comment
Add comment · Show 1
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 rorydriscoll · Mar 08, 2016 at 05:30 AM 0
Share

Did you try logging out the instance id (GetInstanceID) to make sure that you absolutely have just one instance of this script?

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Removing a Component that is a Nested Class. 0 Answers

Why does unity not see monobehaviour? 2 Answers

Why does unity not see monobehaviour? 1 Answer

Unity Bug? null reference exception, Only its not null... 0 Answers

NullReferenceExeption mais variable initialisée 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