Level Complete Script Only works properly on 1 Level

Im just starting Unity, and Im making a modified version of a tutorial I found online. At the end of each level, a cube is set to a trigger that is supposed to run an animation before transporting me to the next level. However, this code only works on the first level, while for the second one it does not run and the 3rd puts me in an infinite loop.

EndTrigger Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EndTrigger : MonoBehaviour
{
public GameManager gameManager;

    void OnTriggerEnter(Collider collisionInfo){
        if(!collisionInfo.GetComponent<Collider>().CompareTag("Obstacle"))
        {
            Debug.Log("WORK");
            gameManager.CompleteLevel();
        }
    }
}

GameManager Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{

    public bool gameEnded = false;
    public GameObject completeLevelUI;
    public float restartDelay = 1f;
    public void EndGame(){

        if(!gameEnded){
            gameEnded = true;
            Debug.Log("GAME OVER");
            Invoke("Restart", restartDelay);
        }
    }

    void Restart(){
        gameEnded = false;
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void CompleteLevel(){
        completeLevelUI.SetActive(true);
    }
}

LevelComplete Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelComplete : MonoBehaviour
{
    void LoadNextLevel(){
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

Anyone know the issue? I also have checked the assets and all of the scripts are where they should be.

Where in the code is LoadNextLevel being called?

If LoadNextLevel is being called, once buildIndex + 1 doesn’t target anything, pretty sure that will cause an error.

But you really need to add Debug.Log calls and find out what code is and isn’t running.

Thanks, it was supposed to be a public void used to an event in an animation, but I forgot to attach it.

Did you add all of the scenes to the build settings? The buildIndex comes from this list and SceneManager can only load scenes that have been added here.

9740614--1393471--upload_2024-3-30_22-57-17.png