- Home /
How to get "name" member of a ScriptableObject, as a SerializedProperty
In a test project I created a simple ScriptableObject:
public class TestObj : ScriptableObject {
public string description;
[MenuItem("Assets/Create/TestObj", false, 10)]
static void CreateTransitionSkin(MenuCommand menuCommand)
{
TestObj newObj = ScriptableObject.CreateInstance<TestObj>();
newObj.name = "NewTestObject";
newObj.description = "A whole bunch of words";
AssetDatabase.CreateAsset(newObj, "assets/NewTestObject.Asset");
}
}
I then created a Custom Editor For the class:
[CustomEditor(typeof(TestObj))]
public class TestObjEditor : Editor
{
public override void OnInspectorGUI()
{
SerializedProperty nameProperty = serializedObject.FindProperty("name");
SerializedProperty descriptionProperty = serializedObject.FindProperty("description");
if (nameProperty == null)
Debug.Log("Could not find property name, which is" + ((TestObj)target).name);
else
EditorGUILayout.PropertyField(nameProperty);
if (descriptionProperty == null)
Debug.Log("Could not find property description");
else
EditorGUILayout.PropertyField(descriptionProperty);
}
}
But when I create and inspect the object, the Editor fails to find the name Property and returns null. The console output is:
Could not find property name, which isNewTestObject
Which shows that the name member exists and contains valid data, it just doesn't work with FindProperty.
Note: The description property IS found, and works/displays as expected.
I suspect the issue has to do with the fact that "name" is an inherited member from ScriptableObject, possibly (edit: confirmed it is) an accessor, and not a field. **What am I doing wrong / how do I make it work? (edit: confirmed it is an accessor, not a field, so that's what I'm doing wrong. Still, how do I make work? Sure I could just use a TextField, but I want all the undo stuff SerializedProperties support.)
This question certainly deserves a +1 as well. Questions like those are rare (well written, hasn't been asked a thousand times before, has actual relevance for others, is actually about Unity).
Answer by villevli · Nov 15, 2017 at 09:48 PM
The serialized property name is "m_Name".
serializedObject.FindProperty("m_Name");
You can see the serialized fields of MonoBehaviour/ScriptableObject when you open your SriptableObject .asset file in a text editor. Editor Settings->Asset Serialization->Mode must be set to Force Text.
Exactly ^^. Great to see more people spreading some basic knowledge about the serialization system.
Answer by eneroth3 · Dec 23, 2018 at 12:48 PM
If I'm not mistaken name is implemented at the Object level, and Unity may therefore not serialize it.
What I know the outdated Hungarian notation has nothing to do with this.
the notation is irrelevant, the exact name of the field is what's needed. It IS serialized, as shown in the answer above, and indicated by the fact that the name persists.
Your answer
Follow this Question
Related Questions
Display Custom Inspectors for each class in a List<> 1 Answer
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
How to work with custom class (ScriptableObject) SerializedProperty? 1 Answer
Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers
SerializedProperty.FindPropertyRelative returns null with ScriptableObjects 4 Answers