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 /
  • Help Room /
avatar image
1
Question by OctaneSyrup · Apr 05, 2019 at 04:38 AM · inspectorreferencemonobehaviourcustom editor

How to select a MonoBehaviour in inspector from a cusom editor?

Hello there. Essentially, one of my scripts needs a reference to another component, and I want to be able to set in manually in the inspector (i.e. dragging and whatnot). This is all fine and good if the MonoBehaviour field is directly in my Component class, like so:

 public class MyComponent : MonoBehaviour {
     public MonoBehaviour Reference;
 }

However, what I want to do is have the reference inside a scriptable object, as a settings objet for MyComponent, like so:

 public class MyComponent : MonoBehaviour {
     public SettingsObject Settings;
 }
 
 public class SettingsObject: ScriptableObject {
     public MonoBehaviour Reference;
     // ... and other fields
 }

As such, I created a fully functional Custom Editor to have the SettingsObject fields displayed in the inspector:

 [CustomEditor(typeof(MyComponent))]
 public class MyComponentEditor : Editor {
     private MyComponent _component;
     private Editor _settingsEditor;
 
     private void OnEnable()
     {
         _component = (MyComponent)target;
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         var settings = _component.Settings;
         if (settings != null)
         {
             using (var check = new EditorGUI.ChangeCheckScope())
             {
                 CreateCachedEditor(settings, null, ref _settingsEditor);
                 _settingsEditor.OnInspectorGUI();
 
                 if (check.changed)
                 {
                     doStuff();
                 }
             }
         }
 }

Visually, the result is mostly the same. In my example, the SettingsObject only contains the reference, but in my project it has a bunch of other fields (of other types, like ints an strings) that get displayed and updated properly and I can adjust.

The issue? I can't select anything for my Reference field. if I hit the bullseye to the right, nothing shows up in the selection window, and when I drag&drop components I can't drop them onto it. I suspect that the "CachedEditor" has something to do with this, but I can't figure out why..

Why does this happen and how can I fix it? Thanks in advance.

EDIT: I also tried both of the following, without an success: CreateCachedEditorWithContext(settings, _component, null, ref _settingsEditor); CreateCachedEditorWithContext(settings, _component.gameObject, null, ref _settingsEditor); I think I'm closer to figuring it out, as "ExposedReference" sounds awfully like something I'm trying to get to.

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

0 Replies

· Add your reply
  • Sort: 

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

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

there MonoBehaviour script data type? 1 Answer

Target field of a MonoBehaviour attribute ? 1 Answer

Custom Inspector for Particle System 2 Answers

error CS0246: The type or namespace name 'MonoScript' could not be found (are you missing a using directive or an assembly reference?) 0 Answers

Show variable of class that dont inherit from MonoBehaviour in Inspector 1 Answer


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