Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
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
Add comment · 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
0
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
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 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. $$anonymous$$arking 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.

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

MissingReferenceException Help 1 Answer

Need help with a raycast 1 Answer

Use variable from another script with WWW give NULL? 1 Answer

Why is this script showing all keycodes except shift? 2 Answers

Why is the Object reference is empty ? 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