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
2
Question by U_Ku_Shu · Oct 12, 2015 at 11:32 PM · c#publicgetsetparameter

C# public parameter with custom get/set doesnt shown in the Inspector

 private bool _showFps;
 public bool ShowFps
         {
             get { return _showFps; }
             set
             {
                 _showFps = value;
                 GameObject.FindGameObjectWithTag("EnvironmentFps").GetComponent<fps>().FpsEnabled = value;
             }
     }

_showFps configured in the Start() from correct Object/component.

Why this Parameter doesn't show in the Inspector?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Xarbrough · Oct 15, 2015 at 04:38 PM

The easiest way is to mark the private backing field "_showFps" as serializable, so it can be saved:

 [SerializeField]
 private bool _showFps;
  public bool ShowFps
          {
              get { return _showFps; }
              set
              {
                  _showFps = value;
                  GameObject.FindGameObjectWithTag("EnvironmentFps").GetComponent<fps>().FpsEnabled = value;
              }
      }

To elaborate: Properties can't show up, because they are methods, not data. Only their backing fields, the regular "variables" store information. This data must also be understood by Unity's serialization system. Unity serializes public variables by default. To tell it to also save private fields, you can use the [SerializeField] attribute.

Comment
Add comment · Show 3 · 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 U_Ku_Shu · Oct 21, 2015 at 07:33 PM 0
Share

In this case if SerializeField showFps will be changed in the editor, there will be no reaction.

avatar image Xarbrough · Oct 21, 2015 at 09:05 PM 0
Share

Sure, but why would you want to turn on fps measuring in edit mode? The setter is used during play mode to active the fps script, but you can still store a persistent "showFps-state" via the serialized field. But maybe I misunderstood, what you wanted to achieve.

avatar image U_Ku_Shu Xarbrough · Oct 21, 2015 at 09:08 PM 0
Share

in any case -- thanks for reply, this solution is also pretty good

avatar image
3

Answer by JoshuaMcKenzie · Oct 13, 2015 at 11:31 AM

If you want to have Properties Show up in the inspector, you'll need to dabble a bit in Editor Scripting.

Since Properties can behave in several different ways, my guess is that having Unity generically show them by default might be a little tricky. Some properties could be "publicly get" but "privately set", or vice versa, which creates all sorts of rendering conditions that would have to be accounted for.

But since you know the context which your properties can be viewed you can easily create a special OnInspectorGUI to properly render your properties

Comment
Add comment · Show 1 · 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 Bunny83 · Oct 15, 2015 at 02:53 PM 0
Share

It's not really about rendering / displaying the property, but about Unity's serialization system. The Inspector only shows things that can be serialized. In general it would not make much sense to display things that aren't serialized since whatever you change won't have any affect if it's not saved.

Unity's serializer only serializes fields at the moment. Properties are actually methods. They don't necessarily hold any data and can have strange side effects when read from / written to. Just like in the example of the OP. If there's no gameobject with the tag "EnvironmentFps" or that object doesn't have an "fps" script attached you would get NullReference exceptions in the editor just by "viewing" the class that contains that property.

In this case when you really want a property that controls the enabled state of the "fps" script, it's better to implement it like this without the redundant bool:

 private fps _fpsScript = null;
 
 public bool ShowFps
 {
     get { return GetFpsScript().enabled; }
     set { GetFpsScript().enabled = value; }
 }
 
 private fps GetFpsScript()
 {
     if (_fpsScript != null)
         return _fpsScript;
     var go = GameObject.FindGameObjectWithTag("EnvironmentFps")
     if (go == null)
         throw new System.Exception("No gameobject with tag 'EnvironmentFps' found");
     _fpsScript = go.GetComponent<fps>();
     if (_fpsScript == null)
         throw new System.Exception("There's no fps script on the gameobject with tag 'EnvironmentFps'");
 }


avatar image
0

Answer by dkjunior · Oct 13, 2015 at 03:17 AM

C# properties don't show up in the inspector, for some reason. Make it a public field instead:

 public bool showFps;
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
avatar image
0

Answer by juanelo · Jun 27, 2020 at 08:33 AM

Did anyone figure out how to make these properties properly serializable / settable through the animator?

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

9 People are following this question.

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

Related Questions

how to set a value in one script and call it in another? (c#) 1 Answer

Public variable hidden in the inspector 2 Answers

When to use SETters and GETters 2 Answers

Are properties variables? 1 Answer

Multiple Cars 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