• 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
Question by MisterYeti · Jun 13, 2019 at 12:19 PM · editorvariable

Expose protected variable in inspector

Hello !

I have two classes :

  public class Slide : MonoBehaviour 
  { protected bool _auto = false; }
  public class VideoSlide : Slide
  {}

The question is : How could I expose the variable "_auto" in the inspector only for VideoSlide objects in the scene ?

I tried to do a Custom Editor, like that :

  [CustomEditor(typeof(VideoSlide))]
  public class VideoSlideEditor : Editor
  {
      public override void OnInspectorGUI()
      {
          base.OnInspectorGUI();
          var script = (VideoSlide)target;
          script.Auto = EditorGUILayout.Toggle("Auto",script.Auto);
          
      }
  }

That works for exposing the variable in the editor, but when I press Play in the editor, the variable always go back to false even if i check the box !

Does someone know why ? :) Thanks for your help!

EDIT : I want to expose the _auto variable ONLY on VideoSlide objects, and not on both Slide & VideoSlides objects, or I would just have [SerializeField] the varible..

Comment

People who like this

0 Show 0
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

Answer by xxmariofer · Jun 13, 2019 at 01:20 PM

there is no need of creatinbg an editor script, the variable is not being shown because "from the point of view of the c$$anonymous$$ld behaves like a private value" just mark the variable in the parent class as serializefield

 public class Slide : MonoBehaviour 
 { [SerializeField] protected bool _auto = false; }
Comment
Bunny83

People who like this

1 Show 6 · 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
avatar image MisterYeti · Jun 13, 2019 at 01:26 PM 0
Share

@xxmariofer The thing is that I don't want to expose the variable in the inspector for Slide objects.

I want to expose _auto ONLY on VideoSlide objects :/

And I feel really sorry because I just saw that I didn't precised that on my question...

avatar image Bunny83 MisterYeti · Jun 13, 2019 at 02:08 PM 1
Share

That's not possible. The variable is declared inside your Slide class. You can not change or alter the declaration in a derived class. Without the SerializeField attribute the field is not serialized at all. If you don't want the variable be serialized / visible in the Slide class, why do you actually declare it inside that class? There's not enough information to recommend a workaround. Maybe declare just a virtual property inside your Slide class and implement the actual field inside the derived class? However this seems to make no sense at all. If the variable should be part of the Slide class and should be serialized it has to be declarerd that way.

avatar image xxmariofer Bunny83 · Jun 13, 2019 at 02:55 PM 0
Share

is there a way of referencing an editor script from inside a regular monobehaviour? since his value is not serialized, at the start of the program the value is setto the default value so, the only solution i can think of is somehow reference the editor script so in the awake you give the var the current state of the toggle(i doubt thats posible), or from inside the editor script create scriptableobjects that work as data serialization for the childs, but thats some extra work

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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

Related Questions

Declaring animations on a script in the editor 1 Answer

Custom Inspector - How to add functionality? 1 Answer

Can someone tell me if its possible to add this into a custom editor to edit values? 0 Answers

"When a value is changed..." 2 Answers

Class Reference in Editor not working 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