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. Dismiss Notice

Question Manual-conforming custom MonoBehaviour inspector errors on entering Play mode?

Discussion in 'UI Toolkit' started by RobertClose, Sep 25, 2023.

  1. RobertClose

    RobertClose

    Joined:
    Nov 9, 2020
    Posts:
    3
    Hi there,

    Sorry if this turns out to have a simple solution, but after getting familiar with making custom inspectors with IMGUI, I am now trying out UIToolkit and following the tutorials in the manual - this page in particular:

    https://docs.unity3d.com/Manual/UIE-HowTo-CreateCustomInspector.html

    The guide first shows referencing a UXML file in the Car_Inspector.CreateInspectorGUI() method using the AssetDatabase.LoadAssetAtPath() method and a hardcoded filename. The guide then explains why this isn't ideal, and instead suggests declaring a public VisualTreeAsset variable within CarInspector, and setting a default reference to the UXML asset in the Inspector instead.

    My issue is that after following this advice and setting up a default reference to my UXML file, I get an error in Play mode. If I select a GameObject containing the MonoBehaviour component that I am making the custom inspector for, the VisualTreeAsset variable is null. I have no error with a hardcoded path technique.

    On the one hand, this seems reasonable, as I think I know and understand why default references don't carry from the Editor into Scenes. On the other hand, it seems then that the recommended way of making a custom inspector with UIToolkit doesn't work for the very common occurence of a custom MonoBehaviour inspector in Play mode?

    My instinct is that surely something on my end has gone wrong and that the guide is fine, but I'm feeling baffled as to what's gone wrong and where to start?
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    969
    This is not normal, the assigned default VisualTreeAsset should still work when entering playmode.

    Which Unity version are you using?
     
  3. RobertClose

    RobertClose

    Joined:
    Nov 9, 2020
    Posts:
    3
    Ahh, I am a bit behind and using 2021.3.5f1 in that project. I understand how UIToolkit might've changed since then. That said, the corresponding 2021 manual still suggests the same default reference technique.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    969
    I had no issues with 2021.3.30f1, worth a shot upgrading. Let us know if you still have issues afterward.