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:
public class TEST : MonoBehaviour
{
public Camera uiCamera;
private void Start()
{
Camera.main.GetComponent<UniversalAdditionalCameraData>()
.cameraStack.Add(uiCamera);
}
}
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:
But if I select any other gameobject before expand Camera component, URP will not throw the exception:
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.b__0 () (at <3d6ac16e3c754a199373804b462a1b72>:0) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
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!
IndexOutOfRangeException: Index was outside the bounds of the array.
UnityEditor.Rendering.Universal.UniversalRenderPipelineSerializedCamera.Update () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.7/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs:113)
UnityEditor.Rendering.Universal.UniversalRenderPipelineCameraEditor.OnInspectorGUI () (at Library/PackageCache/com.unity.render-pipelines.universal@12.1.7/Editor/Camera/UniversalRenderPipelineCameraEditor.cs:333)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Editor/Inspector/InspectorElement.cs:636)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)
Solved the issue. Make sure that the scene which contains the camera, should be active scene via script
Example:
SceneManager.SetActiveScene(SceneManager.GetSceneByName(“MapScene”));
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.
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.
Running into this as well after adding more post processing to my game, latest 2021 LTS. Once the camera is bugged, you are unable to fix it in the inspector because the inspector can no longer load it. None of the suggestions here helped. The only thing I could do was revert my scene using version control. Not a good look Unity.
In Unity 2022.3.20 this error IndexOutOfRangeException: renderPassIndex happened with Pico Ultra 4 using URP when the scene view and game view were open at the same time in the editor. Hiding one of those windows resolved the error.