Ternary Operator

I used Ternary Operator for a different approach on LoadNextLevel function and wanted to share :slight_smile:
Ternary Operator C# - Microsoft

    void LoadNextLevel()
    {
        // Getting current level index
        int currentLevelIndex = SceneManager.GetActiveScene().buildIndex;
        // Calculating the last level index
        int lastLevelIndex = SceneManager.sceneCountInBuildSettings - 1;
        // Calculating next level index
        int nextLevelIndex = currentLevelIndex < lastLevelIndex ? currentLevelIndex + 1 : 0;

        SceneManager.LoadScene(nextLevelIndex);
    }

Good job! :slight_smile:

1 Like

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

Privacy & Terms