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.