Coroutine Error

I am running in to an error that states a CoRoutine couldnt be started because the Object "Script Loader’ is ‘Inactive’. I don’t know what this means.
I am trying us a CoRoutine to increment my stages based on the current scene, but only if that scene = to index scene 4 or higher…

public class SceneLoader : MonoBehaviour
{
    int currentScene;
    [Range(1, 10)] [SerializeField] float loadTime = 5;

    LevelMusic levelMusic;
    AudioSource audioSource;
    // Start is called before the first frame update
    void Start()
    {

        currentScene = SceneManager.GetActiveScene().buildIndex;
        if (currentScene == 0)
        {
            StartCoroutine(LoadMainMenu());
        }
        else if (currentScene <= 1)
        {
            Debug.Log("Current Scene is: '" + SceneManager.GetActiveScene().name + "' .");
            //Tells me what scene I am on, if I don't activate the game 
           //from the Splash Screen.
        }
    }

    //Scene Navigation 
    public void LoadContinue()
    {
        //Checks to see If the current screen's index number is Lower then 4, 
        //if it is, returns the player to the Realm Select Screen.
        //(this allows Game Over Screen Continue Button to Function as intended,   
        //for the moment.) For any scene index above 4, Load (increment the scene) 
        //to the next Level.

        currentScene = SceneManager.GetActiveScene().buildIndex;
        if (currentScene >= 4 )
        {
            StartCoroutine(LoadNextLevel());
        }
        else { SceneManager.LoadScene("3. RealmSelect"); }

    }
    public void LoadRealmSelect() { SceneManager.LoadScene("3. RealmSelect"); }
    public void LoadGameOver() { SceneManager.LoadScene("1. GameOver"); }

   
    //Realm Select Navigation
    public void LoadW1L1() { SceneManager.LoadScene("Level1-1"); }
    public void LoadW2L1() { SceneManager.LoadScene("Level2-1"); }
    public void LoadW3L1() { SceneManager.LoadScene("Level3-1"); }
    public void LoadW4L1() { SceneManager.LoadScene("Level4-1"); }
    public void LoadW5L1() { SceneManager.LoadScene("Level5-1"); }
    public void LoadW6L1() { SceneManager.LoadScene("Level6-1"); }

    //Coroutines
    //loads the StartScreen
    IEnumerator LoadMainMenu()
    {
        yield return StartCoroutine(BeginMusic());
        yield return new WaitForSeconds(loadTime);
        SceneManager.LoadScene("2. MainMenu");
    }
    //Begins LevelMusic scripting
    IEnumerator BeginMusic()
    {
        FindObjectOfType<GameObject>();
        Debug.Log("The Object is" + FindObjectOfType<GameObject>().name + ".");
        audioSource = gameObject.GetComponent<AudioSource>();
        audioSource.volume = .10f;
        audioSource.Play();
        yield return new WaitForSeconds(0);
    }
    //Increments current scene
    IEnumerator LoadNextLevel()
    {   
        currentScene = currentScene++; 
        yield return new WaitForSeconds(0);
    }
}

OK, I was able to partially fix the issue and bypass the CoRoutine, after looking it over again and again, I realized I was being too complicated. However, now I have a strange occurance, where, I have to methods() with the same code. On is within an If Statement, with an else after it, and on is just a straight call.
When SceneManager.LoadScene(currentScene++) is called from LoadCont(); it restarts the same scene, instead of incrementing the scene to the next scene… However, If SceneManager.LoadScene(currentScene++) is called from LoadNextLevel(); it loads the corresponding scene…

public void LoadCont()
    {
        //Checks to see If the current screen's index number is Lower then 4, 
        //if it is, returns the player to the Realm Select Screen.
        //(this allows Game Over Screen Continue Button to Function as intended, 
        //for the moment.) For any scene index above 4, 
        //Load (increment the scene) to the next Level.

        currentScene = SceneManager.GetActiveScene().buildIndex;
        if (currentScene >= 4 )
        {
            SceneManager.LoadScene(currentScene++);
        }
        else { SceneManager.LoadScene("3. RealmSelect"); }

    }

public void LoadNextScene() { SceneManager.LoadScene(currentScene++); }

TLDR: These are my two methods, LoadNextScene() operates as intended. LoadCont() reloads the same scene instead of incrementing to the next level… I dont know why…

I thought of something I thought might work so I tried this… and Now, when I click the continue button, in my scene (4 or above), it hangs for a little continues as nothing happened. Clicking the button on Scene 1 “1. GameOver” functions correctly and loads the realm select screen…

public void LoadCont()
    {
        for (currentScene = currentScene = SceneManager.GetActiveScene().buildIndex; currentScene >= 4; currentScene++)
        {
            if (currentScene < 4)
            {
                SceneManager.LoadScene("3. Realm Select");
            }
        }
        //Checks to see If the current screen's index number is Greater 
        //or equal to 4, if it is, load (increment scene) next level.
        //(this allows Game Over Screen Continue Button to Function as intended, 
        //for the moment.) For any scene index below 4, 
        //returns the player to the Realm Selection

        //currentScene = SceneManager.GetActiveScene().buildIndex;
        //if (currentScene >= 4 )
        //{
        //    SceneManager.LoadScene(currentScene++);
        //}
        //else { SceneManager.LoadScene("3. RealmSelect"); }
    }

Hi,

I’m afraid I don’t understand your idea. If you want to increment a value after a scene was loaded, look up the sceneLoaded delegate and the example in the API.

I think this might be a issue with your trying to use the postfix increment operator while passing the value to SceneManager.LoadScene
So basically when you think you are passing the next value on the line below you are actually passing the currently value of currentScene and before it gets incremented by 1.

SceneManager.LoadScene(currentScene++);
  • The reason it appears to work in the LoadNextScene but not LoadCont is likely that LoadCont was already called once before LoadNextScene which would have incremented currentScene by 1 already. So by the time you do the postfix increment again in LoadNextScene it appears to work although as explained it not working as you intended.

  • To make sure you pass the incremented value first increment it in a previous line and then pass the value or use the prefix increment operator as shown below :

SceneManager.LoadScene(++currentScene);
1 Like

Thank you! I’ll try this.

Thank you for the information. I have been trying a few different approaches I learned while looking through the API, and I think I may be trying something a little more advanced than I anticipated when I set out. But, I have enjoyed the learning process and seeing how different approaches effect the code… I will keep researching, try out some more methods, and update when I have more info. Thank you, again.

THANK YOU!!! This worked!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms