- Home /
Instance gotten with reflection is throwing a null reference when trying to access its fields with reflection.
Hi! I am accessing my instance of animation clip inspector via reflection. I have a tool that is opening this inspector. The below code is getting my instance of the animation clip inspector and is then trying to access its animation controller.
private void Update(){
GetAnimationContoller();
}
private void GetAnimationController(){
var AnimPreviewInspector = Resources.FindObjectsOfTypeAll(typeof(Editor).Assembly.GetType("UnityEditor.AnimationClipEditor"));
if((AnimPreviewInspector[0]==null)==true){
Debug.Log("Your animation preview inspector is null");//this never prints out
}
var test=AnimPreviewInspector[0];
Debug.Log(test);//prints what is expected. UnityEditor.AnimationClipEditor, does not say it is null
var AnimController=typeof(Editor.GetField("m_Controller,System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(test);//this throws a null reference, I have also tried just passing AnimPreviewInspector[0] to the GetValue function
}
As you can see in my comments I am getting a null reference when trying to access the animation controller stored in the animation clip editor. I know the names of these classes and functions by looking at Unity's decompiled dlls. This is the first time I have ever tried to use reflection and am not sure if I'm missing something about reflection or what is going on here. The ultimate goal here is to get the animation controller from my AnimationClipEditor instance, tell what animation is being played when the user clicks the play button, tell what time in the animation the playback is at, and then call a function to do something at specific times. Please help!
Answer by Adam-Mechtley · Apr 27, 2017 at 05:29 PM
Your example didn't come across totally accurate, but it looks like the problem is you are looking for a field m_Controller
on the type Editor
, but that field is not defined on Editor
, it is defined on AnimationClipEditor
. So you need to do something like:
var animationClipEditorType = typeof(Editor).Assembly.GetType("UnityEditor.AnimationClipEditor");
var animationClipEditor = Resources.FindObjectsOfTypeAll(animationClipEditorType)[0];
var controller = animationClipEditorType.GetField("m_Controller", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(animationClipEditor);
Your answer
Follow this Question
Related Questions
How to tell if an animation clip is playing in the editor(NOT in playmode) with an editor script 1 Answer
reflection propertyinfo.getvalue compiles fine but gives erros in editor 1 Answer
Issue with a Mecanim motion reference in synchronized layers 0 Answers
Get current Animation Controller being edited by Animator (In Editor) 0 Answers
How to get reference on current sample from animation window in editor 0 Answers