Search Unity

Bug NullReference at runtime

Discussion in 'UI Toolkit' started by Roshinator, Sep 11, 2020.

  1. Roshinator

    Roshinator

    Joined:
    Feb 2, 2019
    Posts:
    13
    When running my project (Mono on Mac), I get this nullreference that breaks all UI events from firing such as button clicks. The scene has a single UI document with a title and 3 buttons.


    NullReferenceException: Object reference not set to an instance of an object
    at UnityEngine.UIElements.StyleSheets.HierarchyTraversal.Recurse (UnityEngine.UIElements.VisualElement element, System.Int32 depth) [0x00040] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/HierarchyTraversal.cs:21
    at UnityEngine.UIElements.UQuery+UQueryMatcher.TraverseRecursive (UnityEngine.UIElements.VisualElement element, System.Int32 depth) [0x0007e] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:105
    at UnityEngine.UIElements.StyleSheets.HierarchyTraversal.Traverse (UnityEngine.UIElements.VisualElement element) [0x00001] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/HierarchyTraversal.cs:11
    at UnityEngine.UIElements.UQuery+UQueryMatcher.Traverse (UnityEngine.UIElements.VisualElement element) [0x00001] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:75
    at UnityEngine.UIElements.UQuery+UQueryMatcher.Run (UnityEngine.UIElements.VisualElement root, System.Collections.Generic.List`1[T] matchers) [0x00008] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:117
    at UnityEngine.UIElements.UQuery+SingleQueryMatcher.Run (UnityEngine.UIElements.VisualElement root, System.Collections.Generic.List`1[T] matchers) [0x00009] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:128
    at UnityEngine.UIElements.UQueryState`1[T].First () [0x00001] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:221
    at UnityEngine.UIElements.UQueryExtensions.Q[T] (UnityEngine.UIElements.VisualElement e, System.String name, System.String className) [0x00184] in /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/UQuery.cs:1008
    at MainMenuButtonFunctions.Start () [0x0000d] in /Users/username/Documents/Unity/WPT/Assets/Scripts/UI Functions/MainMenuButtonFunctions.cs:23

    (Filename: /Users/username/Documents/Unity/WPT/Library/PackageCache/com.unity.ui@1.0.0-preview.8/Core/HierarchyTraversal.cs Line: 21)
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    We've seen issues like that with the UI Builder corrupting style sheets. To confirm this, can you close the UI Builder window and re-import all your UI files?

    Let us know if that doesn't work and in any case, please do report a bug.
     
  3. Roshinator

    Roshinator

    Joined:
    Feb 2, 2019
    Posts:
    13
    I tried a re-import and it unfortunately did not solve the issue. Just to clarify, this does not happen in editor, it happens in a built standalone player only.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    Thanks for reporting, if you have a project with consistent reproducible steps, we would very much appreciate a bug report (Help > Report a Bug...)
     
  5. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    I had a second look at your issue, what actually happens is that you are trying to query the child of a null element.

    For example, you may use rootVisualElement.Q("foo").Q("bar"). But if a null value is returned for "foo" then you will hit a null reference exception because Q("bar") has an invalid argument.

    Because Q() is an extension method the error is a bit cryptic at the moment. We will fix this to give a proper error but something else is wrong in your project.

    Do share more info with us so we can help -- hopefully this will give you another pointer.
     
  6. Roshinator

    Roshinator

    Joined:
    Feb 2, 2019
    Posts:
    13
    After some further investigation, you were right it was a query issue. We have a build expiration system with a preprocessor that was being set incorrectly and that was causing it to interfere with the menu when it wasn't supposed to, and it is also why it failed only at runtime. Thank you guys so much for your help!