- Home /
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?
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.
In this case if SerializeField showFps will be changed in the editor, there will be no reaction.
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.
in any case -- thanks for reply, this solution is also pretty good
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
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'");
}
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;
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?
Your answer
Follow this Question
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