I'm trying to change the runtime animator of an object when the player starts to drag it around. I do this by subscribing to a custom delegate that's called in `OnBeginDrag` using `IBeginDragHandler`.
I'm sometimes getting a MissingReferenceException error message on the line `_animator.runtimeAnimatorController = AnimatorHead;` and I can't figure out why.
MissingReferenceException: The object of type 'Animator' 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.
The error is not reliably reproducible. Sometimes I'll start the game in the editor and every time I start dragging, I'm getting this error. I'll stop in the game or leave the scene, then hit start again/reload the scene and it works like expected for this run. Sometimes I get the error a few runs in a row, sometimes I can't reproduce it, no matter how often I restart the game/reload the scene.
The strange thing is that the animator is definitly there! I added a `if (_animator == null) Debug.Break();` just before the line that throws the error. So when the error occours, the editor pauses, I look in the inspector and the animator is there! ( `_animator` is a private variable that I made visible by `[SerializeField]` and it is only set once just after Start() and then never assigned to again, so it shouldn't be able to change between the Debug.Break call and the end of the frame)
![script in inspector screenshot][1]
If I double click on the animator it takes me to it and there the animator is in all it's glory, active, enabled, visible...
![animator screenshot][2]
Do you have any idea how this error can occur? Or how I could narrow it down? How do I find out how the animator get's lost, when I can see it in the inspector?
[1]: /storage/temp/200707-script.png
[2]: /storage/temp/200708-animator.png
↧