5

In Unity I've decided to make a custom editor for my component.
The component itself has a list of objects that I've declared as List.

The Editor targets this like this:

myCustomList = serializedObject.FindProperty ("myCustomList");

The problem is that when I try to get/set value of the myCustomList using myCustomList .objectReferenceValue = modifiedCustomList as List< MyCustomObject > tells me that List< MyCustomObject> cannot be cast as Object.

I tried to simply set the values through myCustomList = (target as TargetClass).myCustomList, but (of course) when I pressed the play button the objects instances were reset to a fresh new list.

How do I cast List to an Object? Or how to use the serializedObject to get/set data of types like Lists?

1
  • So to clarify your question, are you asking why you can't cast your modifiedCustomList as a List<MyCustomObject>, meaning you are starting with a list of just objects, and wanting to convert them to your custom object but can't seem to cast them? Aug 1, 2014 at 5:27

1 Answer 1

3

Youll need to iterate through the object like so...

 SerializedProperty myCustomList = serializedObject.FindProperty ("myCustomList");

    for (int i = 0; i < myCustomList .arraySize; i++)
    {
        SerializedProperty elementProperty = myCustomList.GetArrayElementAtIndex(i);

        //Since this the object is not UnityEngine.Object you can not convert them the unity way.  The compiler can determine the type that way so.....

       MyCustomList convertedMCL = elementProperty.objectReferenceValue as System.Object as MyCustomList;
    }

Since SerializedProperty is not a UnityEngine.Object you can not convert them the unity way. The compiler can not determine the type that way.

A discussion on the subject can be found here.

8
  • I'll give this a go, looks very promising! Aug 1, 2014 at 5:39
  • Worked for me. Hope all goes well.
    – Ray Garner
    Aug 1, 2014 at 6:27
  • can't really transform elementProperty's objectReferenceValue to MyCustomObject... Trying to look for a way to do this now Aug 1, 2014 at 7:24
  • I edited the answer to include a way for you to cast to your custom object.
    – Ray Garner
    Aug 1, 2014 at 8:13
  • 1
    The problem was that my value object class and the class that had an extended editor had those fields accessible as internal and thats why it was always returning null Aug 4, 2014 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.