• 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
Question by shaken · Apr 02, 2013 at 09:31 PM · eventnullmissingreferenceexception

Why are my (null) scripts receiving events?

I'm currently developing a piece of software where the user will perform an activity to get a score, and can go back in and perform the same activity again to improve their score.

To load the activity, we simply instantiate a prefab and disable our "player" so the prefab has control.

When the user completes the activity, we destroy the prefab with Destroy, not DestroyImmediate (though I have tried this to see if it has an effect on what I'm witnessing).

My problem is that when the user goes back into the activity there seem to be some "ghost" scripts from the previous activity load that are still receiving events. When I put a breakpoint at the top of the event-receiving functions, it tells me that "this" is null, so the script itself is null. And of course I get a MissingReferenceException, presumably because the object that held the script isn't around anymore.

I have tried to search around for an answer to why this is happening, and in my search I found someone stating that when Destroy is called, the gameObject is destroyed, but the scripts are not. Or at least they aren't destroyed immediately. From what I understand they are supposed to be cleaned up at the end of the frame where Destroy was called, presumably to give them time to finish their Updates.

Has anyone else encountered this issue? Does anyone know of a way to be sure the scripts get destroyed? It is possible that I am just doing something horribly wrong and don't realize it, but the problem seems pretty straightforward to me.

Thanks.

Comment

People who like this

0 Show 3
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 shaken · Apr 02, 2013 at 10:19 PM 0
Share

I just tested your suggestion - unsubscribing from the events in OnDestroy - and that did indeed work. So the scripts don't receive the events because they have unsubscribed, but I fear that the ghost scripts themselves may still be hanging around. I worry that memory accumulation may be an issue if these ghost scripts keep getting created and hanging around.

Thank you for the workaround though - it has helped me move on in implementation at the very least. :)

avatar image mhtraylor · Apr 02, 2013 at 10:23 PM 0
Share

You're welcome. That's why I didn't want to post that as answer because it still doesn't take care of those ghost scripts hanging around :) Edit: Since Components can be removed with Destroy(), it might be possible to ensure the scripts are removed first by using Destroy() on them before Destroy() on their gameObject?

avatar image Benproductions1 · Apr 02, 2013 at 11:24 PM 0
Share

@mhtraylor could you convert your comment to an answer, so this question can be closed :)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by mhtraylor · Apr 02, 2013 at 10:09 PM

Is it possible in your situation to have the scripts unsubscribe from the Event(s), say in a call to some *.OnDestroy() method? This might be a workaround, but forgive me if I am totally off-base here.

Comment

People who like this

0 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 shaken · Apr 03, 2013 at 03:41 PM 0
Share

From a comment you posted above, mhtraylor: "Edit: Since Components can be removed with Destroy(), it might be possible to ensure the scripts are removed first by using Destroy() on them before Destroy() on their gameObject?"

We updated our clean up code to 1) deactivate parent 2) go through all components in children and destroy them 3) go through all children and destroy them. I then removed your suggested workaround to test if this change to the clean up code solved the issue of ghost scripts receiving events, and it did. Marking this as the answer. Thanks again.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Missing objects From Build 1 Answer

Null reference when calling a method from another script 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Runtime get data from script component with missing script 1 Answer

Have null errors 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