Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Level Complete Script Only works properly on 1 Level

Discussion in 'Scripting' started by Np3_, Mar 31, 2024.

  1. Np3_

    Np3_

    Joined:
    Oct 21, 2023
    Posts:
    3
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EndTrigger : MonoBehaviour
    6. {
    7. public GameManager gameManager;
    8.  
    9.     void OnTriggerEnter(Collider collisionInfo){
    10.         if(!collisionInfo.GetComponent<Collider>().CompareTag("Obstacle"))
    11.         {
    12.             Debug.Log("WORK");
    13.             gameManager.CompleteLevel();
    14.         }
    15.     }
    16. }
    17.  
    GameManager Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.  
    9.     public bool gameEnded = false;
    10.     public GameObject completeLevelUI;
    11.     public float restartDelay = 1f;
    12.     public void EndGame(){
    13.  
    14.         if(!gameEnded){
    15.             gameEnded = true;
    16.             Debug.Log("GAME OVER");
    17.             Invoke("Restart", restartDelay);
    18.         }
    19.     }
    20.  
    21.     void Restart(){
    22.         gameEnded = false;
    23.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    24.     }
    25.  
    26.     public void CompleteLevel(){
    27.         completeLevelUI.SetActive(true);
    28.     }
    29. }
    30.  
    LevelComplete Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LevelComplete : MonoBehaviour
    7. {
    8.     void LoadNextLevel(){
    9.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    10.     }
    11. }
    12.  
    Anyone know the issue? I also have checked the assets and all of the scripts are where they should be.
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Where in the code is LoadNextLevel being called?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,192
    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.
     
  4. Np3_

    Np3_

    Joined:
    Oct 21, 2023
    Posts:
    3
    Thanks, it was supposed to be a public void used to an event in an animation, but I forgot to attach it.
     
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    152
    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.

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