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
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 () {...}
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.
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.