Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 arisonu123 · Apr 27, 2017 at 03:03 PM · animationeditoreditor-scriptinganimator controllerreflection

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!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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