Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question 2d struct array causes error

Discussion in 'Scripting' started by kinglotrich, Sep 8, 2023.

  1. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    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
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,156
    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.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194
    Or this hack... make your data
    Thang
    be:

    Code (csharp):
    1. // @kurtdekker - cheap and cheerful way to name
    2. // array elements in a Unity Editor Inspector array
    3. [System.Serializable]
    4. public class Thang
    5. {
    6.     public string Name;
    7.     public int Age;
    8. }
    and declare a collection thereof:

    Code (csharp):
    1. public Thang[] MyThangs;
    And off you go!

    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!
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,156
    I mean they specifically mentioned that in their post and how they didn't want to do that.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194
    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.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,156
    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.
     
    Kurt-Dekker likes this.
  7. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    how can I make a editor script to inject my presets for element names, would you help ?
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,156
    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.
     
  9. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    339
    I think that if the first member is a string it just automatically uses the value as the element name in the inspector.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194
    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<T>() (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.
     
    Bunny83 and CodeRonnie like this.