- Home /
Script component disappears randomly in runtime
I have already searched for it, but all I can find is about this error appearing when the game is played or something like it. That's not my case.
My game works fine for some time, but then, randomly, I get this error:
"The referenced script on this Behaviour is missing!"
After that I get some NullReferenceException. Apparently my script components disappear.
This has happened to more than one script, and I can't see any link between them.
What could be causing this?
I have this happening right now. I use ProcedureLightning and the asset prefab is removed from the hierarchy. When I stop the runtime the prefab is there back in place.
Answer by Bunny83 · Apr 15, 2019 at 12:07 AM
This happens when you have a script attached to an object (in the scene or a prefab) and you
deleted your actual script file from your project.
you moved it to a different folder outside of Unity without the corresponding meta file.
you renamed either the class or the file the class is defined in so the script can no longer be used.
To fix this problem, first make sure your script and the file it contains both have the same name. Now you have two options:
Either just delete the old component from your gameobjects and re-add the script.
If you originally had many settings and you don't want to loose those settings, you can keep the broken component and re-link the script file. To do this switch your inspector into debug mode (context menu at the top right of the inspector). Once in debug mode your component should have a script field. Now you can simply drag the correct script onto that variable and the component should work again. Just don't forget to switch the inspector back to normal mode.
I found this: onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity. A comment was in this: https://stackoverflow.com/questions/4449955/activity-ondestroy-never-called
I have a performance problem. This is why this is see$$anonymous$$gly random. I have narrowed down an operation scheme and can cause it to happen on purpose. Now I can adjust operational parameters to quiet the issue down to a tolerable level in Unity. Or this will just become an apparent thing that I have to circumvent in another fashion.
No, no, no. You're completely off-topic here. Please stop posting on completely off topic threads. When we talk about OnDestroy or destroying gameobjects we talk about within the Untiy environment. What you're referencing with that SO question is pure Android OS and activity stuff. Yes, when you build for android Unity will be an activity within the Android OS. However the OS does not destroy individual things inside an activity. The OS will kill inactive / suspended activities if it requires memory. However activities are either killed completely or not.
Your answer
Follow this Question
Related Questions
Why am I getting a NullReferenceException while trying to set a gameobjects animation frame? 0 Answers
NullReferenceException .. problem with Camera.main.transform 2 Answers
Raycasting null object reference error 1 Answer
Editing in Animator removes Player tag 0 Answers
NullReferenceException: Object reference not set to an instance of an object 3 Answers