2d struct array causes error

I make 2d struct array to hold event infos works fine but whenever ı add new element in arrays this error occur but when I restart game everthing get fixed is it normal ?
and is there anyone who know how can I change element 0,1 etc names to something more meaningful. I know ı can add string name but this time I have to write all of them by one by. for expemle they can get their struct name and order number like atk 1, atk 2, it would be awesome.

NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
UnityEditor.SerializedProperty.get_objectReferenceInstanceIDValue () (at <347e3e2bef8c4deb82c9790c6e198135>:0)
UnityEditor.EditorGUIUtility.ObjectContent (UnityEngine.Object obj, System.Type type, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator) (at <347e3e2bef8c4deb82c9790c6e198135>:0)
UnityEditor.UIElements.ObjectField+ObjectFieldDisplay.Update () (at <347e3e2bef8c4deb82c9790c6e198135>:0)
UnityEditor.UIElements.ObjectField.UpdateDisplay () (at <347e3e2bef8c4deb82c9790c6e198135>:0)
UnityEngine.UIElements.VisualElement+SimpleScheduledItem.PerformTimerUpdate (UnityEngine.UIElements.TimerState state) (at :0)
UnityEngine.UIElements.TimerEventScheduler.UpdateScheduledEvents () (at :0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at :0)
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at :0)
UnityEditor.RetainedMode.UpdateSchedulers () (at <27a779fc555e412cad6318e4bfb44443>:0)


image866×547 17 KB

Unity can’t serialise 2d arrays, though from the image it doesn’t look like you are using one anyway.

Nonetheless the error seems like an editor bug. Having custom label names for the list will involve a custom editor.

Or this hack… make your data Thang be:

// @kurtdekker - cheap and cheerful way to name
// array elements in a Unity Editor Inspector array
[System.Serializable]
public class Thang
{
    public string Name;
    public int Age;
}

and declare a collection thereof:

public Thang[] MyThangs;

And off you go!

9282697--1300687--Screen Shot 2023-09-08 at 8.34.26 PM.png

I couldn’t believe it when I saw that posted a month or two ago. Wow. Cheesy-easy!

I mean they specifically mentioned that in their post and how they didn’t want to do that.

1 Like

Didn’t read that…

That complaint about not wanting to type it in… I would never hand-type everything in either. I’d make a little editor script to inject your presets of editable names from your whatever, enum, database, filestore, whatever

Or write an Editor Script, either way. But writing custom drawers when this suffices. Meh.

Admittedly I didn’t realise that the labels in your screenshot had updated to reflect the fields contents. That’s a really weird thing to be embedded in the list drawer. I wonder what the exact conditions for qualifying for it are. Just a member called ‘Name’?

OP is in a newer version of Unity that’s using the UI Elements ListView visual element, however, so this feature may not carry over.

Really wish Unity would add some attributes to make modifying the drawers of some visual elements a lot easier, such as Odin Inspector’s [ListDrawerSettings] attribute that lets you modify their (far better) collection drawer.

1 Like

how can I make a editor script to inject my presets for element names, would you help ?

It’s a pretty deep topic, honestly. Custom inspectors and property drawers aren’t super newbie friendly, though Unity has tried to make the documentation more comprehensive on them.

Tutorial in the manual: https://docs.unity3d.com/Manual/UIE-HowTo-CreateCustomInspector.html
Custom editor docs: https://docs.unity3d.com/ScriptReference/Editor.html
Property Drawer docs: https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

Don’t be too put off if it seems a bit daunting as it’s very much intermediate+ territory.

I think that if the first member is a string it just automatically uses the value as the element name in the inspector.

I concur with this. Doesn’t seem to matter what the string variable is actually named.

After brief experiments, the rules I can infer are:

  • if the first field (in code-appearing order) is a string, and it is non-null and non-empty, the editor picks it up as a label.

  • if subsequent fields are strings, doesn’t matter.

  • if the first field isn’t a string, labels come out as Element 0, etc.

Neato.

The approach above is still NOT how I would label stuff! If you did, then you are on the hook for finding it by string as well in code, which is just a disaster waiting to happen, keeping those strings in sync.

This super-basic data authoring use case is purpose-built for ScriptableObjects: just use the actual filename of the ScriptableObject itself as the primary key.

The benefit of that is if you put all the ScriptableObject instances in a folder, the operating system prohibits you from having duplicate primary keys.

With clever dynamic loading via Resources.LoadAll() (or Addressables if you insist), you never even drag any of them into anywhere. You just edit them, add them, and the code can discover them all at runtime. It is glorious.

2 Likes