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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug IndexOutOfRangeException in URP

Discussion in 'Universal Render Pipeline' started by SolarianZ, Jul 8, 2022.

  1. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    215
    In Unity 2021.3.0f1 and URP 12.1.6, when adding a Camera to a Camera Stack in a script may trigger an IndexOutOfRangeException.

    I write a script to add camera to camera stack during Start() method.


    Code:
    Code (CSharp):
    1. public class TEST : MonoBehaviour
    2. {
    3.     public Camera uiCamera;
    4.  
    5.     private void Start()
    6.     {
    7.         Camera.main.GetComponent<UniversalAdditionalCameraData>()
    8.                 .cameraStack.Add(uiCamera);
    9.     }
    10. }
    If I select the camera gameobject(which owns the Stack) and then enter Play Mode, and then expand Camera component in Inspector window, URP will throw a IndexOutOfRangeException:
    upload_2022-7-8_23-39-56.gif

    But if I select any other gameobject before expand Camera component, URP will not throw the exception:
    upload_2022-7-8_23-40-29.gif

    Error Log:
    IndexOutOfRangeException: Index was outside the bounds of the array.
    UnityEditor.Rendering.Universal.UniversalRenderPipelineSerializedCamera.Update () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.6/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs:113)
    UnityEditor.Rendering.Universal.UniversalRenderPipelineCameraEditor.OnInspectorGUI () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.6/Editor/Camera/UniversalRenderPipelineCameraEditor.cs:278)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <3d6ac16e3c754a199373804b462a1b72>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)
     
  2. ChristmasGT

    ChristmasGT

    Joined:
    Sep 21, 2011
    Posts:
    11
    Hey there! Just wanted to confirm that I'm having the exact same issue. Can reproduce and replicate exactly as you've described as well (Using 2021.3.4f1). Thanks for showing the work around in the meantime!

    Code (CSharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. UnityEditor.Rendering.Universal.UniversalRenderPipelineSerializedCamera.Update () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.7/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs:113)
    3. UnityEditor.Rendering.Universal.UniversalRenderPipelineCameraEditor.OnInspectorGUI () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.7/Editor/Camera/UniversalRenderPipelineCameraEditor.cs:333)
    4. UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Editor/Inspector/InspectorElement.cs:636)
    5. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)
    6.  
     
  3. WilsonCWong

    WilsonCWong

    Joined:
    Mar 20, 2013
    Posts:
    32
    Also seeing this on 2021.3.11f1.
     
    irfanyigitbaysal likes this.
  4. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Also seeing this on 2021.3.13f1.
     
  5. irfanyigitbaysal

    irfanyigitbaysal

    Joined:
    Sep 3, 2020
    Posts:
    2
    I have exactly the same problem in 2021.3.11f1. Cannot add stack via script
     
  6. irfanyigitbaysal

    irfanyigitbaysal

    Joined:
    Sep 3, 2020
    Posts:
    2
    Solved the issue. Make sure that the scene which contains the camera, should be active scene via script
    Example:
    SceneManager.SetActiveScene(SceneManager.GetSceneByName("MapScene"));
     

    Attached Files:

  7. hmfff

    hmfff

    Joined:
    Dec 19, 2017
    Posts:
    2
    same issure, its the bug code in URP

    for (int i = 0; i < numCameras; ++i)
    {
    cameraSerializedObjects.Update();
    }
     
  8. hmfff

    hmfff

    Joined:
    Dec 19, 2017
    Posts:
    2
    If u not avtive chosen the base main camera, and running ,it will not show the error.
    if u focus the main camera, it will error.
    its the editor serilize update bug.
     
  9. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    161
    the problem keeps in 2022.3.7f1.
    I try to insert one overlay cam via script to enable overlay cam only when really necessary but I receive the same error.