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 URP Light2D working in Editor, not on Windows build

Discussion in 'Editor & General Support' started by lynxmmo, Oct 7, 2023.

  1. lynxmmo

    lynxmmo

    Joined:
    Aug 22, 2021
    Posts:
    2
    Hello,

    On my project I add a simple Light2D Spot object. It displays correctly while running the project through Editor however as soon as I run the build, I get the error below and the world is not rendering.

    I really think that I setup URP correctly for my project. Can someone help?

    Thanks a lot!

    Direct3D:
    Version: Direct3D 11.0 [level 11.1]
    Renderer: NVIDIA RTX A4500 (ID=0x2232)
    Vendor: NVIDIA
    VRAM: 20264 MB
    Driver: 31.0.15.3699
    Begin MonoManager ReloadAssembly
    - Loaded All Assemblies, in 0.261 seconds
    - Finished resetting the current domain, in 0.002 seconds
    <RI> Initializing input.
    <RI> Input initialized.
    <RI> Initialized touch support.
    Direct3D: detected that using refresh rate causes time drift. Will stop trusting refresh rate until the game window is moved.
    Direct3D: detected that vsync is broken (it does not limit frame rate properly). Delta time will now be calculated using cpu-side time stampling until the game window is moved.
    UnloadTime: 0.764200 ms
    NullReferenceException: Object reference not set to an instance of an object
    at UnityEngine.Rendering.Universal.Light2D.GetTopMostLitLayer () [0x00052] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.RendererLighting.CanCastVolumetricShadows (UnityEngine.Rendering.Universal.Light2D light, System.Int32 endLayerValue) [0x00000] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.RendererLighting.RenderLightVolumes (UnityEngine.Rendering.Universal.IRenderPass2D pass, UnityEngine.Rendering.Universal.RenderingData renderingData, UnityEngine.Rendering.CommandBuffer cmd, System.Int32 layerToRender, System.Int32 endLayerValue, UnityEngine.Rendering.RenderTargetIdentifier renderTexture, UnityEngine.Rendering.RenderTargetIdentifier depthTexture, UnityEngine.Rendering.RenderBufferStoreAction intermediateStoreAction, UnityEngine.Rendering.RenderBufferStoreAction finalStoreAction, System.Boolean requiresRTInit, System.Collections.Generic.List`1[T] lights) [0x0008f] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.Render2DLightingPass.DrawLayerBatches (UnityEngine.Rendering.Universal.LayerBatch[] layerBatches, System.Int32 batchCount, System.Int32 startIndex, UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData, UnityEngine.Rendering.FilteringSettings& filterSettings, UnityEngine.Rendering.DrawingSettings& normalsDrawSettings, UnityEngine.Rendering.DrawingSettings& drawSettings, UnityEngine.RenderTextureDescriptor& desc) [0x00445] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.Render2DLightingPass.Execute (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x00150] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.ExecuteRenderPass (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.ScriptableRenderPass renderPass, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x0009a] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.ExecuteBlock (System.Int32 blockIndex, UnityEngine.Rendering.Universal.ScriptableRenderer+RenderBlocks& renderBlocks, UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData, System.Boolean submit) [0x00030] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.Execute (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x0026a] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.RenderSingleCamera (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.CameraData& cameraData, System.Boolean anyPostProcessingEnabled) [0x0017f] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.RenderCameraStack (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Camera baseCamera) [0x00346] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.Render (UnityEngine.Rendering.ScriptableRenderContext renderContext, System.Collections.Generic.List`1[T] cameras) [0x00094] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.RenderPipeline.InternalRender (UnityEngine.Rendering.ScriptableRenderContext context, System.Collections.Generic.List`1[T] cameras) [0x0001c] in <a8fc136049044b19982b5abcb6f6b426>:0
    at UnityEngine.Rendering.RenderPipelineManager.DoRenderLoop_Internal (UnityEngine.Rendering.RenderPipelineAsset pipe, System.IntPtr loopPtr, UnityEngine.Object renderRequest) [0x00046] in <a8fc136049044b19982b5abcb6f6b426>:0

    Render Pipeline error : the XR layout still contains active passes. Executing XRSystem.EndLayout() right now.
    NullReferenceException: Object reference not set to an instance of an object
    at UnityEngine.Rendering.Universal.Light2D.GetTopMostLitLayer () [0x00052] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.RendererLighting.CanCastVolumetricShadows (UnityEngine.Rendering.Universal.Light2D light, System.Int32 endLayerValue) [0x00000] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.RendererLighting.RenderLightVolumes (UnityEngine.Rendering.Universal.IRenderPass2D pass, UnityEngine.Rendering.Universal.RenderingData renderingData, UnityEngine.Rendering.CommandBuffer cmd, System.Int32 layerToRender, System.Int32 endLayerValue, UnityEngine.Rendering.RenderTargetIdentifier renderTexture, UnityEngine.Rendering.RenderTargetIdentifier depthTexture, UnityEngine.Rendering.RenderBufferStoreAction intermediateStoreAction, UnityEngine.Rendering.RenderBufferStoreAction finalStoreAction, System.Boolean requiresRTInit, System.Collections.Generic.List`1[T] lights) [0x0008f] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.Render2DLightingPass.DrawLayerBatches (UnityEngine.Rendering.Universal.LayerBatch[] layerBatches, System.Int32 batchCount, System.Int32 startIndex, UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData, UnityEngine.Rendering.FilteringSettings& filterSettings, UnityEngine.Rendering.DrawingSettings& normalsDrawSettings, UnityEngine.Rendering.DrawingSettings& drawSettings, UnityEngine.RenderTextureDescriptor& desc) [0x00445] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.Render2DLightingPass.Execute (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x00150] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.ExecuteRenderPass (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.ScriptableRenderPass renderPass, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x0009a] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.ExecuteBlock (System.Int32 blockIndex, UnityEngine.Rendering.Universal.ScriptableRenderer+RenderBlocks& renderBlocks, UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData, System.Boolean submit) [0x00030] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.ScriptableRenderer.Execute (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.RenderingData& renderingData) [0x0026a] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.RenderSingleCamera (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Rendering.Universal.CameraData& cameraData, System.Boolean anyPostProcessingEnabled) [0x0017f] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.RenderCameraStack (UnityEngine.Rendering.ScriptableRenderContext context, UnityEngine.Camera baseCamera) [0x00346] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.Universal.UniversalRenderPipeline.Render (UnityEngine.Rendering.ScriptableRenderContext renderContext, System.Collections.Generic.List`1[T] cameras) [0x00094] in <e7a7940e73ef46e89121b2923ce7d407>:0
    at UnityEngine.Rendering.RenderPipeline.InternalRender (UnityEngine.Rendering.ScriptableRenderContext context, System.Collections.Generic.List`1[T] cameras) [0x0001c] in <a8fc136049044b19982b5abcb6f6b426>:0
    at UnityEngine.Rendering.RenderPipelineManager.DoRenderLoop_Internal (UnityEngine.Rendering.RenderPipelineAsset pipe, System.IntPtr loopPtr, UnityEngine.Object renderRequest) [0x00046] in <a8fc136049044b19982b5abcb6f6b426>:0
     
  2. Encon21

    Encon21

    Joined:
    Nov 27, 2018
    Posts:
    4
    Just ran into this issue myself, in my case it was caused by adding a Light2D via AddComponent, because the m_ApplyToSortingLayers field (shown as "Target Sorting Layers" in the inspector) is never set. Unity defaults it to "Everything" if the value is null on Awake, but only in the editor (https://github.com/Unity-Technologi...ipelines.universal/Runtime/2D/Light2D.cs#L489), which is why it breaks in the player.

    I wasn't able to find a way to set the value normally during runtime, but I only looked into it for a few minutes so I may have overlooked something. Seems odd that there wouldn't be a way to do it though, given that the field seems to be required for the component to work at all, unless Unity doesn't want us using AddComponent to spawn lights during runtime. It's possible to set it via reflection but I'm not sure if that would potentially cause other problems, and I'd rather avoid using reflection to do it if possible. Hoping someone else has a proper solution for this.

    For reference I'm currently using Unity 2022.3.10f1 and URP 14.0.8.
     
    lynxmmo likes this.
  3. lynxmmo

    lynxmmo

    Joined:
    Aug 22, 2021
    Posts:
    2
    Thanks for reply! Just for records, I am using Unity 2022.3.7f1 & URP 14.0.8
     
  4. Nintynstor

    Nintynstor

    Joined:
    Dec 12, 2022
    Posts:
    9
    Hey there,That error can be a real head-scratcher. It seems like your Light2D Spot object is causing issues in the build. First, double-check your Universal Render Pipeline (URP) settings in the build. Sometimes, settings don't carry over perfectly.Also, ensure you're using the correct URP version compatible with your Unity version. Mismatched versions can cause funky problems.Lastly, examine your build settings. Sometimes, specific platforms or build configurations can cause rendering hiccups.Debugging this might take a bit of trial and error, but you'll get there. Happy coding! ️‍♂️️
     
  5. Szkeptik

    Szkeptik

    Joined:
    Aug 30, 2016
    Posts:
    7
    My latest build started throwing pretty much this exact error. It started right after I upgraded from 2021.3.24f1 to 2022.3.11f1.
    When I run an earlier build built with the previous unity version 2021.3.24f1 the error doesn't happen.
    I have URP14.0.9

    EDIT: For me it was not with Using ADDcomponent, but something related to light layers and how they interact. When I set everything to only affect the Default layer the error didn't happen again, but I'm not sure what was the exact issue.
     
    Last edited: Oct 21, 2023