- Home /
Type casting SerializedProperty.objectReferenceValue doesn't work?
public class Achievement
{
public string description;
public bool status;
}
public class AchievementsManager : MonoBehaviour
{
public Achievement ach = new Achievement();
}
////////////////////////////THE PART WHERE I GET MY PROBLEM///////////////////////////////
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor (typeof(AchievementsManager))]
public class AchievementsEditor : Editor
{
SerializedObject m_object;
SerializedProperty m_achievement;
public void OnEnable ()
{
m_object = new SerializedObject(target);
m_achievement = m_object.FindProperty("ach");
}
public override void OnInspectorGUI ()
{
m_object.Update();
**//////////////I cannot type cast the m_achievement.objectReferenceValue in the next line to Achievement. BUt i saw something like that in a video//////////**
var temp = m_achievement.objectReferenceValue as Achievement;
m_object.ApplyModifiedProperties();
}
}
Answer by Jamora · Jan 27, 2014 at 04:20 PM
Achievement is not a derivative of UnityEngine.Object
; it derives from System.Object
, and thus the compiler cannot convert them porperly.
To fix this, you have the rather strange, non-Unity way of:
Achievement temp = m_achievement.objectReferenceValue as System.Object as Achievement;
or alternatively make your Achievement
derive from ScriptableObject.
Oops sorry. I get this problem after using system.object.
type is not a supported pptr value.
It was the non-Unity way so was to be expected something would break... Seems like the only way now is to make the Achievement-class a ScriptableObject
public class Achievement : ScriptableObject
This also means you must create each instance of Achievement with ScriptableObject.CreateInstance();
Can you give me a small example. That would be really helpful.
Why do you need to cast? I ran into this problem where I wanted to add an item to a serialized property list (of System.Object values) - but then I found out that I don't need to explicitly create an object and add it to the list, I could just do list.arraySize++;
If you need to perform some kind of operation on your object, then consider using target ins$$anonymous$$d, after you're doing do a serializedObject.Update();
(or whatever SerializedObject your property lives in) - you might have to register undo manually then, but it works.
Answer by pl7890 · Apr 24, 2016 at 08:02 AM
You can get the property from the target:
Achievement temp = (AchievementsManager)target.ach as Achievement;
After your operations on temp, you can serialize target again and copy the value to serialzedObject:
serializedObject.CopyFromSerializedProperty(new SerializedObject(target).FindProperty("ach"));
Answer by Billy4184 · Dec 13, 2019 at 10:38 PM
If you just want to display the properties, you can use:
SerializedProperty descriptionProperty = m_achievement.FindPropertyRelative("description");
EditorGUILayout.PropertyField(descriptionProperty);
Your answer
Follow this Question
Related Questions
How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer
Editor: How to do PropertyField for List elements? 4 Answers
Editor Targets are updated but SerializedPropertys arent 1 Answer
Can you have custom EditorGUILayout fields? 0 Answers
Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer