Need example of unboxing data token for custom UdonSharpBehaviour class

I am using DataList to store my “CustomClass”.
I add to the list with myDataList.Add(this);

When I try to unbox it, I lose reference to class members;
I unbox it with var item = (CustomClass)(System.Object)myDataList[0];

Debug.Log(item) shows the object name correctly. However, Debug.Log(item.testInt) or Debug.Log(item.gameboject) result in error.

Is there an example showing how to unbox properly?

Datatokens do not become your data, they contain your data. You need to get the reference out with .Reference first, before attempting to cast it to your type. (CustomClass)myDataList[0].Reference should work.

Alternatively, in udonsharp you can make a static extension method like this

    public static T GetReference<T>(this DataToken token)
        return (T)token.Reference;

And then you can do myDataList[0].GetReference<CustomClass>() which is a little nicer.

1 Like