First off, I would like to say that I am new to Unity and still trying to wrap my head around the scripting system. With that I have a simple question regarding member accessors and having the value displayed in the inspector viewport. Question simply put, I have noticed that if a class member is set as public it’s value will be displayed within the inspector viewport.
But with my limited C# experience I am understanding, possibly wrongly, that this while not exactly wrong is a bad practice and if possible things should be handled by an accessor with basic error checking. Though when doing so it seems to be impossible to have it’s default value displayed within the inspector. Is this an absolute, or is there a tag that can be placed above the accessor to have values displayed in the inspector?
Here is an example where I could have something set with a default texture, and using the C# script may change it later to something at a later time. It is not claimed to be anywhere near functioning or even using the right syntax, but simply an example. As a side note, I did take a look briefly through UnityAnswers and wasn’t able to find an answer. Any insight into this is appreciated, thank you in advance for your time!
public class Foo : MonoBehaviour
{
...
private Texture2D _img;
...
//!---
// Possible to attach tag to display in inspector?
public Texture2D SpiffyTexture
{
get
{
// Is member valid
if(this._img != null)
{
// Return member
return this._img;
}
else
{
// Inform log of invalid member
print("Reference to private member 'img' is invalid, returning null object.");
// Return null object
return null;
}
}
set
{
// Is passed value valid
if(value != null)
{
// Set member to passed value
this._img = value;
}
else
{
// Inform log of invalid value
print("Passed value to private member 'img' is invalid, value has not changed.");
}
}
...
public void DoTextureStuffs()
{
...
Texture2D tmp = new Texture2D(128, 128);
SpiffyTexture = tmp.LoadImage(ProcTextureLib.Generate("BrickWorn"));
}
}