Custom Inspector: Accessing other classes using serializedProperty

Hi there, I am following along with the live unity tutorial about editor scripting and I am having an issue replicating what they are doing.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/editor-basics/editor-scripting-intro

I am trying to access an array of classes called BehaviourData within a custom editor for another script. Much like is done in the tutorial linked above.

Below is my code for the class BehaviourData

using UnityEngine;
using System.Collections;

[System.Serializable]
public class BehaviourData{

    public Transform target;

    public BehaviourData(Transform t)
    {
        target = t;
    }
}

and then in my custom inspector script i have this method

private  BehaviourData[] GetBehaviourDataArray()
    {
        var arrayCount = behaviourCount.intValue;
        BehaviourData[] transformArray = new BehaviourData[arrayCount];
      
        for (var i=0 ; i<arrayCount;i++)
        {          
            transformArray[i] = serializedObject.FindProperty(string.Format(behaviourTypeArrayData,i)).objectReferenceValue as BehaviourData;
        }
      
        return transformArray;
    }

which mirrors the tutorial but I get compiler errors saying that they can not convert UnityEngine.Object to BehaviourData via a reference conversion, boxing conversion etc etc.

I have tried multiple times to convert in different ways including “as System.Object as BehaviourData” but this brings a whole host of other issues.
I have checked my code against the tutorial several times and besides their “Waypoint” method (which isnt ever shown) there are no differences other than names in our scripts. I am quite lost here

Shouldn’t be too hard to have access to an array of an object from a custom inspector
Kind Regards
Keelan

1 Like

You didn’t show what is behaviourCount or behaviourTypeArrayData and how you get a reference to them.

Assuming these are all fine, your issue is that FindProperty returns an instance of SerializedProperty.
I like to think of the SerializedObject and SerializedProperty classes as “views” on top of actual objects that allow you (and the serialization system) to view an object as a generic serializable type, iterating over its properties and fields, etc.

Using objectReferenceValue on this property returns an instance derived from UnityEngine.Object. It would probably work in case your object derived from that.

So, a quick fix would be to have BehaviourData derive from UnityEngine.Object.

Thanks for this reply. Yeah to be honest I dont really have a great knowledge on editor scripting. Mostly just find examples and modify the solutions to my needs. This helped my understanding quite a bit, so thanks.

How exactly would one make a class derive from UnityEngine.Object?
Would you just write it like this?
public class Example : UnityEngine.Object () {...}

Again thanks for your insight.

Yes, exactly.

It’s not possible to derive your own classes directly from UnityEngine.Object. You have to either derive them from MonoBehaviour (i.e. make them be components), or derive them from ScriptableObject.

1 Like

I didn’t realize that.

BTW why is there no API for retrieving the target object instance from SerializedProperty for non “Object” types? (like i asked in the beta group today).

The serialization system recognizes these objects so it should also provide a way to return the underlying instance IMO.

Probably the answer is ‘nobody wrote it yet,’ but one complication is that the underlying object may not actually exist; one of the features of the serialisation system is the ability to manipulate stored data without actually having to construct any managed objects.

i knew you would copy/paste the “nobody wrote it yet response” !! :slight_smile:

1 Like