- Home /
Is it way to force redraw all components of GO in inspector?
Subj. I have some script manages adding/removing others scripts on its GameObject. But when I remove some component, I got error:
MissingReferenceException: The object of type 'script_you_just_remove' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
I belive, it's because Unity Inspector "don't know" that removed component does not exists anymore and try to draw it at current iteration. So, I guess I must somehow repaint all components. Is there any way to do it? Thanks.
That is most likely not the issue, Unity knows the component has been destroyed because it is Unity that destroys it when you call the Destroy function.
If you check the entire error message it gives you a script and a line number where the error occurs. This is most likely in one of your scripts which is referencing the script you removed, as the error says.
At first I thought so, but unfortunately it is an issue. In my script (manager) there is only two references to component: adding and destroying. According to debugger error causes between end of OnInspectorGUI function and its beginning. So I guess I must redraw whole Inspector manually. Or, maybe, it's Unity 4.6 bug.
Showing some scripts from where it says the error happens could help a lot. =)
To redraw the inspector you can use the Repaint function of the Editor class. You could also try to use the EditorGUIUtility.ExitGUI() after you destroy the component to force Unity to start drawing from the top again, although it should not be necessary.
Sure I know that showing code is a best practice, but there is nothing to see, honestly :) It's just AddComponent by pressing button and DestroyImmidiate by presing other one.
But EditorGUIUtility.ExitGUI() works fine, error no longer appears. Thanks a lot!
Answer by Eastwing · Dec 11, 2014 at 10:28 PM
EditorGUIUtility.ExitGUI() is exactly what I need. Thank you, GameVortex!
This doesn't work for me. It just leaves me with 2 errors. Now I also have: "ExitGUIException: Exception of type 'UnityEngine.ExitGUIException' was thrown.".
Do you call EditorGUIUtility.ExitGUI() immediately after DestroyImmediate()? Or do you call it somewhere else?
Your answer
Follow this Question
Related Questions
Refreshing custom Inspector window while playing 1 Answer
Custom "Can't add due to conflicting component" 1 Answer
Custom editors for component/class attributes 1 Answer
2D Animation does not start 1 Answer
Sloppy HingeJoints 0 Answers