Stack Overflow Error: I can't figure out why it is happening,Stack Overflow Error. Can't figure out why it is happening.
Hello, I am currently working on a game in where when you click the start button it changes camera. however when I click the button ti does nothing but log, 'StackOverflowException: The requested operation caused a stack overflow.' I have never ran into this problem before. here is the code. using UnityEngine.SceneManagement; using UnityEngine;
 public class sceneManager : MonoBehaviour {
 
     public void OpenSceneAsync (int sceneIndex)
     {
         SceneManager.LoadSceneAsync(sceneIndex);
     }
     //This is what matters
     public void ChangeCameraDisplay (int cameraIndex) {
         ChangeCameraDisplay(cameraIndex);
     }
 }
 
Thank you for taking the time to help!
,Hello, I was making a menu and when you click start it is meant to call the function 'ChangeCameraDisplay()' however when ever I click it it does nothing and says 'StackOverflowException: The requested operation caused a stack overflow.' can you please help me out? here is the script. using UnityEngine.SceneManagement; using UnityEngine;
 public class sceneManager : MonoBehaviour {
     
     //This isn't the function
     public void OpenSceneAsync (int sceneIndex)
     {
         SceneManager.LoadSceneAsync(sceneIndex);
     }
     //This is the function
     public void ChangeCameraDisplay (int cameraIndex) 
     {
         ChangeCameraDisplay(cameraIndex);
     }
 }
Thank you so much for taking your time to help!
Answer by ZzaAakK_Gaming · Oct 14, 2020 at 05:30 PM
Hi friend - the problem here is that you have infinite recursion. The method 'ChangeCameraDisplay' calls itself, but doesn't do anything else. Chances are you probably want an array/list of cameras that you can switch between, then activate the one you want based on 'cameraIndex'. Something like this:
 public class sceneManager : MonoBehaviour 
 {
 
     //List of cameras - can be added to in the inspector
     public List<Camera> cameras = new List<Camera>();
 
     //your existing method
     public void OpenSceneAsync (int sceneIndex)
     {
         SceneManager.LoadSceneAsync(sceneIndex);
     }
 
     //new method for changing cameras
     public void ChangeCameraDisplay (int cameraIndex)
     {
         for (i = 0; i < cameras.Count(); i++)
         {
             cameras[i].enabled = i == cameraIndex;
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                